Default Parameters in Nested Templates ?

M

Michael Stembera

I would like to use default parameters in nested templates but MS VC++
7.1 chokes on it. Does anyone know how to fix the simple example
below or if indeed it is possible?


template <int N=7>
class A
{
};

template < template <int N=7> class T >
class B
{
A<> a_default; // A works fine using 7 as the default value

T<5> t_5; // T works fine using 5 explicitly

T<> t_default; // error C2976: 'T' : too few template arguments
// why is the default value 7 not getting used here ?
};

int main(int, char**)
{
B< A > b;

return 0;
}

Thanks in advance!
Michael
 
V

Victor Bazarov

Michael Stembera said:
I would like to use default parameters in nested templates but MS VC++
7.1 chokes on it. Does anyone know how to fix the simple example
below or if indeed it is possible?


template <int N=7>
class A
{
};

template < template <int N=7> class T >

Did you actually mean to say

template<class T = A<7> >

???
class B
{
A<> a_default; // A works fine using 7 as the default value

T<5> t_5; // T works fine using 5 explicitly

T<> t_default; // error C2976: 'T' : too few template arguments
// why is the default value 7 not getting used here ?

I don't think the syntax you tried is actually allowed...
};

int main(int, char**)
{
B< A > b;

return 0;
}

Victor
 
R

Richard Smith

Michael said:
I would like to use default parameters in nested templates but MS VC++
7.1 chokes on it. Does anyone know how to fix the simple example
below or if indeed it is possible?

template <int N=7>
class A {};

template < template <int N=7> class T >
class B
{
A<> a_default; // A works fine using 7 as the default value

T<5> t_5; // T works fine using 5 explicitly

T<> t_default; // error C2976: 'T' : too few template arguments
// why is the default value 7 not getting used here ?


From the Standard itself, it's not clear whether or not his
should work. There is a defect report (#150, part 2) on
this. The "rationale" section of the DR says

| Default arguments are allowed for the parameters of a
| template template parameter, and those default arguments
| alone will be considered in a specialisation of the
| template template parameter within a template defintion;
| any default arguments for the parameters of teh template
| template argument are ignored.

This means that your example should compile, and should use
the default argument, 7. To clarify which 7 this is picking
up, let me slightly modify your example:

template <int N = 7> class A {};
template < template <int N = 5> class T > class B {
A<> a; // uses A<7>
T<> t; // uses A<5>
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top