By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

Default Parameters in Nested Templates ?

P: n/a
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Michael Stembera" <m_********@yahoo.com> wrote...
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
Jul 22 '05 #2

P: n/a
Michael Stembera wrote:
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>
};

--
Richard Smith

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.