c++: Passing const int to template function
I am using the rapidxml lib. It defines a function to parse files in this way:
template<int Flags>
void parse(Ch *text)
The lib provides const int
flags for example:
const int parse_declaration_node = 0x20;
So I created a pointer to a static int in my class:
const int * parser_mode;
And in the class constructor I assigned it its value:
parser_mode = &rapidxml::parse_declaration_node;
Then when I try to use this const int *
as template argument to the parse function:
tree->parse<parser_mode>(file->data());
I get this error message:
error: ‘GpxSectionData::parser_mo开发者_Go百科de’ cannot appear in a constant-expression
This rest of the statement seems correct since:
tree->parse<0>(file->data());
doesn't produce compilation error...
Could you please tell me what I am missing here? Thank you!
Thanks to the explanations below I will probably define it out of the class: So I think this is:
class Myclass {
static const int parser_mode;
[...]
}
static const int Myclass::parser_mode = rapidxml::parse_declaration_node;
template<int Flags> void parse(Ch *text)
... const int * parser_mode;
Your template takes an int
as a template parameter, but you are passing it an int*
. The types int
and int*
are not the same.
Try tree->parse<rapidxml::parse_declaration_node>(file->data());
You cannot use variable for value of template parameter.
Instead you can add Flags template parameter to your class
template<int Flags>
class CClass {
//...
};
And set Flags parameter for class instance
CClass<rapidxml::parse_declaration_node> obj;
精彩评论