0

I have a program, which has many ways to assign some parameters (variables) in a code. For example, I can get a parameter from input file and, in the same time, set it manually from command line. If neither input file, nor command line set the parameter I should print a warning message and use default value.

I'm looking for the way in C++ to implement such functionality. Form my point of view it should be class like this

Template <typename T>
class c_assigned_value 
{
protected:  
  bool _assigned;
  T    _value;
public:
...
};

But I cannot find any implementation. Do you have any suggestion?

user2794512
  • 51
  • 1
  • 1
  • 3
    Perhaps *write one* ? – WhozCraig Sep 19 '13 at 08:21
  • 2
    Hello and welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). You might also want to learn what a [SSCCE](http://sscce.org/) is. – Some programmer dude Sep 19 '13 at 08:22
  • @stefan This is _not_ optional, since the value is valid regardless of whether it was assigned or not. – James Kanze Sep 19 '13 at 08:26
  • You should use many overloaded constructors. – ST3 Sep 19 '13 at 08:33
  • @ST3 I don't think that's a solution, since he wants _one_ object, which is present from the start of the program (probably with static lifetime). – James Kanze Sep 19 '13 at 08:39

2 Answers2

2

What's the problem? The constructor sets m_assigned to false, and m_value to the default. If the value is later set, then m_assigned is set to true, and at some point in time, you check m_assigned, and emit a warning if it is false.

I might add that it is a bad idea to start a symbol name with an _. Many implementations treat such symbols as being in their namespace, and define them as macros. (Incorrectly, but that doesn't help when they do it.) More generally, for readability, you should avoid a _ at either end of the symbol.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Let's be honest, the only things [actually reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) are underscore followed by capital letter and double underscore. Single underscore followed by lowercase is only reserved in the global scope. I mean starting symbols with an underscore is generally not a good idea but it's not forbidden in all cases. – PeterT Sep 19 '13 at 19:57
  • @PeterT True according to the standard, but in practice... At least in the past, a lot of system files have used macros starting with an underscore and a lower case. (Of course, some even have macros with no underscore. I've been hit once or twice by such.) – James Kanze Sep 20 '13 at 08:14
0

You should try boost:optional. It does exactly what you want to do.

Manual: http://www.boost.org/doc/libs/1_54_0/libs/optional/doc/html/index.html

Caladan
  • 1,471
  • 11
  • 13
  • This has the added benefit of being the inspiration for the C++14 [std::optional](http://en.wikipedia.org/wiki/C%2B%2B14#Optional_values). – PeterT Sep 19 '13 at 17:18
  • Except that `optional` doesn't do what he wants, since the value must be valid regardless. – James Kanze Sep 20 '13 at 08:15