1

I am trying to both corroborate my understanding and perhaps find a more complete solution to this.

Use-case:

Program has to parse a config file that specifies various parameters, if some parameters are missing the default values defined in a header should be used. The object should be created and intialised once at the start and then be available to all objects through the program till its termination. Nothing should be able to change the values inside that object.

I've looked at several questions and there seems to be lots of conflicting solutions, from using singletons to just static const objects. I know the latter will cause duplication as each single unit will have a copy of the content.

So far part of the solution seems to be using extern as shown in c++ global object in combination with declaring it 'const'.

Also, I use c++11. But I welcome answers about future standards (if applicable).

Questions:

1) Can I also make it static?

2) Where should I initialise this static const globalVal?

3) Does this make sense? Are there better ways? I saw that I could also create an object that only gives a const reference to its content. Would that be better?

4) Is this a good way of dealing with this problem? Just want to make sure I am not totally off.

unixsnob
  • 1,685
  • 2
  • 19
  • 45
  • 2
    Best practice is not to use it (static const). You'll most likely encounter static deinit fiasco. Singletons make it worse. What's wrong with passing around config - like, will the program crash if you have two distinct configs in memory? – lorro Sep 29 '17 at 12:17
  • You could use enums held in a namespace which would prevent namespace pollution. Having a bunch of globals is usually a terrible sign. Could config be a member of your objects that has a default constructor? – Simon Hobbs Sep 29 '17 at 12:42
  • The info has to come from a parsed file. It could be anything, from floats to strings. This values have to be constant for the whole program to operate correctly. For example, some parameters are calibration parameters so they cannot change. Also, I would like to avoid copies. – unixsnob Sep 29 '17 at 13:44
  • @lorro: I explicitly inititialise the variable by calling a parse function before the main `run` function is called in main. – unixsnob Sep 29 '17 at 13:47
  • @unixsnob: I'm not sure I get it... is it a global or a function static const var? Also, no matter where you init it, you'll very likely encounter issues in DEinit: think of accessing a static during static deinit (destruction, i.e. 'after main'). This can happen directly, across multiple fn calls. As long as you have more than one static const that's accessible during static deinit, you're balancing between UB land and segfault. – lorro Sep 29 '17 at 14:02
  • @lorro: In main I create an object that runs the entire program with many threads. When the program terminates, that object is destroyed and control is return back to main so it can end. This static object should be created before that object (with threads ) is created so it can be accessed by all subsequent threads (reading purposes). Once the program is terminating, there should be no threads accessing the global object as they will stop and return to main. – unixsnob Sep 29 '17 at 14:27
  • @lorro: Also, I think I better clarify. The parse function is called from the constructor of the global object. It parses a .txt file, sets all the values and then remains available (read only) for the entirety of main's life. – unixsnob Sep 29 '17 at 14:29
  • @unixsnob: Are you OK with _none_ of your statics having non-trivial dtor (note: this also prohibits std::string)? – lorro Sep 29 '17 at 14:37

0 Answers0