c++ - How to create a compile time constant instance of a structure in C++11 -
in current project trying create compile time const static instance of structure c++11. code compiles , works fine in clang (apple llvm version 6.1.0) not work g++ (gcc) 4.8.3. gcc throws error parameter not declared 'constexpr'. there way make work in gcc?
//parameter.h struct par { const int uniqid; const char *name; const char *description; constexpr par(int uid,const char * n,const char * d) :uniqid(uid), name(n),description(d){} }; class parameters { const static par param_a; ... const static par param_z; printparameters(std::vector<par> parameters); }; //parameter.cpp constexpr par parameters::param_a={0,"-a","bla a"}; ... constexpr par parameters::param_z={0,"-z","bla z"}; void parameters::printparameters(std::vector<par> parameters){ (size_t = 0; < parameters.size(); i++) { switch (parameters[i].uniqid) { case param_a.uniqid: std::cout << "a" << std::endl; break; } }
the gcc following:
parameters.cpp:11:39: error: declaration of ‘const par parameters::param_a’ outside of class not definition [-fpermissive] parameters.cpp:12:39: error: redeclaration ‘parameters::param_a’ differs in ‘constexpr’ constexpr par parameters::param_a={...}; in file included parameters.cpp:2:0: parameters.h:190:34: note: ‘parameters::param_a’ not declared 'constexpr' const static par param_a;
i think problem declare param_a
, param_z
const
in parameter.h , define them constexpr
in parameter.cpp. const
, constexpr
different things. in parameter.cpp must have this:
//parameter.cpp const par parameters::param_a={0,"-a","bla a"}; ... const par parameters::param_z={0,"-z","bla z"};
Comments
Post a Comment