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

Calling Function template

6 views
Skip to first unread message

Alex Vinokur

unread,
Jul 15, 1999, 3:00:00 AM7/15/99
to gen...@graphics.stanford.edu
"GC-Patel, Mehul" wrote:

> We can use
> template<class T> void f2 ()
> {
> }
>
> you are using void in above try to use T and see.
>
> Mehul.
>

[snip]

Hi,

Here is a program with
template <typename T> T foo ();

Alex


//#########################################################
//------------------- C++ code : BEGIN -------------------
#include <iostream>

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

template <typename T>
T foo2 ()
{
cout << __PRETTY_FUNCTION__ << " : ";
T t;
return t;
}
};

// Note! __PRETTY_FUNCTION__ is predefined variable in gcc/g++/egcs compiler

int main ()
{
AAA aaa;
// aaa.foo1<int> (); // WRONG
aaa.template foo1<int> ();
(&aaa)->template foo1<int> ();

// cout << aaa.foo2<int> (); // WRONG
cout << aaa.template foo2<int> () << endl;
cout << (&aaa)->template foo2<int> () << endl;
cout << aaa.template foo2<float> () << endl;
cout << (&aaa)->template foo2<float> () << endl;

return 0;
}

//------------------- C++ code : END ----------------------

//#########################################################
//------------------- Running Results : BEGIN -------------

void AAA::foo1<int>()
void AAA::foo1<int>()
int AAA::foo2<int>() : 0
int AAA::foo2<int>() : 0
float AAA::foo2<float>() : 0
float AAA::foo2<float>() : 0
double AAA::foo2<double>() : 0

//------------------- Running Results : END ---------------


//#########################################################
//------------------- Compiler & System ------------------

g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)

uname -a : SunOS <nodename> 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5

//---------------------------------------------------------

//#########################################################


0 new messages