Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Default template arguments in function templates

1 view
Skip to first unread message

Alex Vinokur

unread,
Mar 17, 1999, 3:00:00 AM3/17/99
to
In article <36EE3145...@cs.utu.fi>,
Jaakko =?iso-8859-1?Q?J=E4rvi?= <jaakko...@cs.utu.fi> wrote:
> Hello,
>
> Default template arguments are not currently allowed in function
> templates.
[snip]


Hi,

Here is an example. We see that
1. when calling
template function gets its missing template-type
according to its argument type.
2. when creating object (of template class)
template class gets its missing template-type
according to the class template default.

Conclusions. 1. There is no difference between calling functions according
to the following declarations : template <typename T = A> void foo1
(-something-) {} template <typename T> void foo2 (-something-) {} (See
functions : f1 () and f2 (); f3 () and f5 (); f4 () and f6 ()).

2. There is the difference between creating objects
according to the following declarations :
template <typename T = A> class COO1 () {}
template <typename T> class COO2 () {}
(See classes : C1 and C2; C3 and C5; C4 and C6).


Thanks,
Alex


//######################################################
//################# 1. main.C ##########################
//######################################################

#include <string>

class A {};
A a;
class B {};
B b;

//======================================
template <typename T = A>
void f1 () { cout << __PRETTY_FUNCTION__ << endl; }
// __PRETTY_FUNCTION__ is predefined variable in GNU gcc/g++

template <typename T>
void f2 () { cout << __PRETTY_FUNCTION__ << endl; }


template <typename T = A>
void f3 (T x = a) { cout << __PRETTY_FUNCTION__ << endl; }

template <typename T = A>
void f4 (T x) { cout << __PRETTY_FUNCTION__ << endl; }


template <typename T>
void f5 (T x = a) { cout << __PRETTY_FUNCTION__ << endl; }

template <typename T>
void f6 (T x) { cout << __PRETTY_FUNCTION__ << endl; }
//======================================

//======================================
template <typename T = A>
class C1 { public : C1 () { cout << __PRETTY_FUNCTION__ << endl;} };

template <typename T>
class C2 { public : C2 () {cout << __PRETTY_FUNCTION__ << endl;} };

template <typename T = A>
class C3 { public : C3 (T x = a) {cout << __PRETTY_FUNCTION__ << endl;} };

template <typename T = A>
class C4 { public : C4 (T x) {cout << __PRETTY_FUNCTION__ << endl;} };

template <typename T>
class C5 { public : C5 (T x = a) {cout << __PRETTY_FUNCTION__ << endl;} };

template <typename T>
class C6 { public : C6 (T x) {cout << __PRETTY_FUNCTION__ << endl;} };


//======================================


int main ()
{
//===============
// f1 (); ## no matching function for call to `f1 ()'
// f1<> (); ## no matching function for call to `f1 ()'
f1<A> ();
f1<B> ();
//===============
// f2 (); ## no matching function for call to `f2 ()'
// f2<> (); ## no matching function for call to `f2 ()'
f2<A> ();
f2<B> ();
//===============
// f3 (); ## no matching function for call to `f3 ()'
// f3<> (); ## no matching function for call to `f3 ()'
f3<A> ();

f3 (a); // f3 (a) is f3<A> (a) according to the a type
f3<> (a); // f3 (a) is f3<A> (a) according to the a type
f3<A> (a);

f3 (b); // f3 (b) is f3<B> (b) according to the b type
f3<> (b); // f3 (b) is f3<B> (b) according to the b type
f3<B> (b);
//===============
// f4 (); ## no matching function for call to `f4 ()'
// f4<> (); ## no matching function for call to `f4 ()'
// f4<A> (); ## no matching function for call to `f4 ()'

f4 (a); // f4 (a) is f4<A> (a) according to the a type
f4<> (a); // f4 (a) is f4<A> (a) according to the a type
f4<A> (a);

f4 (b); // f4 (b) is f4<B> (b) according to the b type
f4<> (b); // f4 (b) is f4<B> (b) according to the b type
f4<B> (b);
//===============
// f5 (); ## no matching function for call to `f5 ()'
// f5<> (); ## no matching function for call to `f5 ()'
f5<A> ();

f5 (a); // f5 (a) is f5<A> (a) according to the a type
f5<> (a); // f5 (a) is f5<A> (a) according to the a type
f5<A> (a);

f5 (b); // f5 (b) is f5<B> (b) according to the b type
f5<> (b); // f5 (b) is f5<B> (b) according to the b type
f5<B> (b);
//===============
// f6 (); ## no matching function for call to `f6 ()'
// f6<> (); ## no matching function for call to `f6 ()'
// f6<A> (); ## no matching function for call to `f6 ()'

f6 (a); // f6 (a) is f6<A> (a) according to the a type
f6<> (a); // f6 (a) is f6<A> (a) according to the a type
f6<A> (a);

f6 (b); // f6 (b) is f6<B> (b) according to the b type
f6<> (b); // f6 (b) is f6<B> (b) according to the b type
f6<B> (b);
//===============
// C1 c10; ## `C1' undeclared (first use this function)
C1<> c11; // c11 is C1<A>() according to the template default
C1<A> c12;
C1<B> c13;
//===============
C2<A> c21;
C2<B> c22;
//===============
C3<> c31; // c31 is C1<A>() according to the template default
C3<A> c32;

C3<> c33 (a); // c33 is C1<A>() according to the template default
// C3<> c34 (b); ## no matching function for call to `C3<A>::C3 (B &)
C3<A> c35 (a);

C3<B> c36 (b);
//===============
C4<> c41 (a); // c41 is C1<A>() according to the template default
C4<A> c42 (a);

C4<B> c43 (b);
//===============
C5<A> c51;

C5<A> c52 (a);

C5<B> c53 (b);
//===============
C6<A> c61 (a);

C6<B> c62 (b);
//===============
return 0;
}


//######################################################
//########### 2. The results of the running ############
//######################################################

void f1<A>()
void f1<B>()
void f2<A>()
void f2<B>()
void f3<A>(class A = a)
void f3<A>(class A = a)
void f3<A>(class A = a)
void f3<A>(class A = a)
void f3<B>(class B = a)
void f3<B>(class B = a)
void f3<B>(class B = a)
void f4<A>(class A)
void f4<A>(class A)
void f4<A>(class A)
void f4<B>(class B)
void f4<B>(class B)
void f4<B>(class B)
void f5<A>(class A = a)
void f5<A>(class A = a)
void f5<A>(class A = a)
void f5<A>(class A = a)
void f5<B>(class B = a)
void f5<B>(class B = a)
void f5<B>(class B = a)
void f6<A>(class A)
void f6<A>(class A)
void f6<A>(class A)
void f6<B>(class B)
void f6<B>(class B)
void f6<B>(class B)
C1<A>::C1<A>()
C1<A>::C1<A>()
C1<B>::C1<B>()
C2<A>::C2<A>()
C2<B>::C2<B>()
C3<A>::C3<A>(class A = a)
C3<A>::C3<A>(class A = a)
C3<A>::C3<A>(class A = a)
C3<A>::C3<A>(class A = a)
C3<B>::C3<B>(class B = a)
C4<A>::C4<A>(class A)
C4<A>::C4<A>(class A)
C4<B>::C4<B>(class B)
C5<A>::C5<A>(class A = a)
C5<A>::C5<A>(class A = a)
C5<B>::C5<B>(class B = a)
C6<A>::C6<A>(class A)
C6<B>::C6<B>(class B)

//######################################################
//########### 3. Compiler ##############################
//######################################################
g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)


//######################################################
//########### 4. System ################################
//######################################################
uname -a : SunOS <nodename> 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5

//######################################################
//########### END of INFORMATION #######################
//######################################################

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

0 new messages