views:

172

answers:

3

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 ?

+3  A: 

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.

anon
Same goes for MSVC: C2976: 'tree_parse_info' : too few template arguments
Nikola Smiljanić
Same for Intel: `too few arguments for class template "tree_parse_info"`
David Rodríguez - dribeas
+4  A: 

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.

GMan
His question says he has no default parameters.
anon
@Neil: Perhaps there is one above his actual definition, in a forward-declaration. It's only a guess, admittedly.
GMan
A: 

Thanks GMan, that makes things clear. There is a forward declaration indeed.

veeru
If you found my answer helpful, please upvote and/or accept it :) (Accept by clicking the green arrow.) Additionally, you can just comment on my answer instead of post a new one.
GMan