Reputation: 201
I'm mimicking the code here: http://www.boost.org/doc/libs/1_59_0/doc/html/program_options/tutorial.html#idp343130416. I want to rewrite the code in a class, using static members to implement the functionality.
However, I found I can't initialize non-copyable static member variable in class. For example, in the class, the following code doesn't work:
class ProgramOptions{
private:
static po::options_description config("Generic options");
}
// Possible reason: visual studio treat it as function declaration.
class ProgramOptions{
private:
static po::options_description config = po::options_description config("Generic options");
}
// Possible reason: in C++, only the int type can be done this way.
// For other types, static variable can't be evaluated where it's declared.
class ProgramOptions{
private:
static po::options_description config;
static void InitializeSaticMemberVariables()
{
generic = po::options_description("Generic options");
}
}
// warning C4512: assignment operator cannot be generated.
// Possible reason: options_description is not copyable and the operator = has been intentionally disabled.
I searched many pages still failed to solve it. What should I do?
I don't want to manage the members in the non-static way, since it's weird to have many instances of program options.
Upvotes: 0
Views: 576
Reputation: 409404
The first alternative should work if your compiler supports C++11 and brace-initialization:
static po::options_description config{"Generic options"};
Note that you still need to actually define the variable as well.
The second variant could possibly be used, but you initialize the variable in the definition:
class ProgramOptions{
private:
static po::options_description config;
...
};
...
po::options_description ProgramOptions::config = po::options_description config("Generic options");
However, this breaks the non-copyable point, as it will use the copy-constructor.
Upvotes: 1