tags:

views:

135

answers:

2
template<class T1, class T2 = int> class A;
template<class T1 = int, class T2> class A;

which is equal to this?

 1)    template<class T1 = int, class T2 = int> class A;   

 2)    template<class T1, class T2> class A;

I know 1st option is correct. according to the standards But ,for me in various compilers 1st option is not accepting giving

  error: redefinition of default argument //for 1st case

and 2nd option is accepting

If any one clarify my doubt..by checking this..Whether i made mistake or else standard is correct...

+2  A: 

The second line you have is invalid, you can't have a template parameter with a default value followed by one without a default value (same as regular parameters).

Comeau online gives this error:

"ComeauTest.c", line 1: error: default argument not at end of parameter list
template <class T = int, class Y>


Taking into consideration UncleBens' comment I see that the first two lines are equivalent to option 1, but you can't repeat it and must use option 2 in order to define the class.

As a style consideration I don't see why either options is needed, I would go with option 1 without the first two lines.

The following compiles and runs fine (in GCC)

#include <iostream>
template <class T, class Y=int> class A;
template <class T=int, class Y> class A;

int main() { 
    A<>* pa = 0;
    std::cout << pa;
}
Motti
@Motti : IAm asking for those two statements is equal to 1 or 2 case....I know what u said ...and what u said is correct..But iam not asking that
BE Student
@BE Student, in that case I don't understand the question please try to clarify.
Motti
The problem is that you are trying to compile this line in isolation. The default for the second argument is given on the first line, and C++ allows spreading out the default definitions for different arguments like that (although it might be considered bad taste).
UncleBens
template<class T, class U = int> class A;template<class T = float, class U> class A;template<class T, class U> class A { public: T x; U y;};A<> a;output:a.x is float, and the type of a.y is int.But iam asking why can't we use...the 1st case instead of second case here...in this example
BE Student
+1  A: 
template<class T1, class T2 = int> class A;
template<class T1 = int, class T2> class A;

template <class T1, class T2> A {};

A<> var; // == A<int, int> var

The effect of the two declarations is that both arguments have int for default, but the language doesn't allow you to redefine any of the arguments, to avoid

template <class T = int> class A;
template <class T = char> class A;
//etc

Option 1 breaks this rule and the compilers that reject 1) are correct.

UncleBens