Default parameters of struct templates
I have a template struct tree_parse_info declared as follows:
template <
typename IteratorT,
typename NodeFactoryT,
typename T
>
struct tree_parse_开发者_开发问答info
{
// ...
};
The compiler allows the follows code:
tree_parse_info<> m_info;
Why does this code compile even though we do not have default template parameters for the template struct tree_parse_info ?
If the class has been forward declared previously, you won't need to restate the default parameters. For example:
// forward declare only
template <typename T = int, size_t N = 10>
struct array;
// actual definition
template <typename T , size_t N>
struct array
{};
int main(void)
{
array<> a; // uses the defaults it saw in the forward-declaration
}
Look above your actual definition to see if you forward declared it.
By the way, if you give defaults at one place, and differing defaults at another, you'll get a compile error:
template <typename T = int, size_t N = 10>
struct array;
// error: redefinition of default parameter
template <typename T = double , size_t N = 2>
struct array
{};
Try giving the code you showed us defaults that can't possibly accidentally match, like:
struct stupid_special_tag_type_lolroflwat {};
template <
typename IteratorT = stupid_special_tag_type_lolroflwat,
typename NodeFactoryT = stupid_special_tag_type_lolroflwat,
typename T = stupid_special_tag_type_lolroflwat
>
struct tree_parse_info
{
// ...
};
If you get redefinition errors, you know you've given it defaults in some other location.
When I compile your code with g++, I get the following error:
tp.cpp:10: error: wrong number of template arguments (0, should be 3)
So you must be compiling something other than the code you are showing us.
精彩评论