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

Explicit template instantiation from template function doesn't compile?

P: n/a
Hi,

I'm trying to explicitly instantiate a template function using the
following syntax:

obj.template_func<type>(params);

It compiles OK when used from a regular function, but it doesn't
compile when used in a template function. Is there any particular
reason for this, or is this a compiler bug? I'm using GCC 3.2.3 on
Debian Linux.

An example program follows here:

//---------------------------------------------------------------
#include <iostream>

using namespace std;

class Test
{
public:
template<class T> void DoIt(int i)
{
T aT = 0;

cout << aT << " " << i << endl;
}
};

class Test2
{
public:
template<class T> void CallIt(int i)
{
Test aTest;

//THIS DOESN'T COMPILE: aTest.DoIt<T>(i);
}

void CallIt2(int i)
{
Test aTest;

aTest.DoIt<float>(i); // HERE IT COMPILES OK
}
};

int main()
{
Test2 aTest;

//aTest.CallIt(1);
aTest.CallIt2(2);

return 0;
}
//---------------------------------------------------------------

In this example, the CallIt function doesn't compile, but CallIt 2,
which uses the same syntax, but it's not a template itself, compiles
fine.

The error message I get is:

tmplt.cpp: In member function `void Test2::CallIt(int)':
tmplt.cpp:24: syntax error before `;' token

Thanks in advance!

Fernando Cuenca.
(mail to: fernando_cuenca *at* yahoo *dot* com *dot* ar)
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Fernando Cuenca wrote:
Hi,

I'm trying to explicitly instantiate a template function using the
following syntax: ....
In this example, the CallIt function doesn't compile, but CallIt 2,
which uses the same syntax, but it's not a template itself, compiles
fine.


FYI - gcc 3.4.0 compiles the snippet without error.
Jul 22 '05 #2

P: n/a
"Fernando Cuenca" <no**************@yahoo.com> wrote...
I'm trying to explicitly instantiate a template function using the
following syntax:

obj.template_func<type>(params);

It compiles OK when used from a regular function, but it doesn't
compile when used in a template function. Is there any particular
reason for this, or is this a compiler bug? I'm using GCC 3.2.3 on
Debian Linux.

An example program follows here:

//---------------------------------------------------------------
#include <iostream>

using namespace std;

class Test
{
public:
template<class T> void DoIt(int i)
{
T aT = 0;

cout << aT << " " << i << endl;
}
};

class Test2
{
public:
template<class T> void CallIt(int i)
{
Test aTest;

//THIS DOESN'T COMPILE: aTest.DoIt<T>(i);
Have you tried

aTest.template DoIt<T>(i);

??
}

void CallIt2(int i)
{
Test aTest;

aTest.DoIt<float>(i); // HERE IT COMPILES OK
}
};

int main()
{
Test2 aTest;

//aTest.CallIt(1);
aTest.CallIt2(2);

return 0;
}
//---------------------------------------------------------------

In this example, the CallIt function doesn't compile, but CallIt 2,
which uses the same syntax, but it's not a template itself, compiles
fine.

The error message I get is:

tmplt.cpp: In member function `void Test2::CallIt(int)':
tmplt.cpp:24: syntax error before `;' token

Thanks in advance!

Fernando Cuenca.
(mail to: fernando_cuenca *at* yahoo *dot* com *dot* ar)

Jul 22 '05 #3

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
"Fernando Cuenca" <no**************@yahoo.com> wrote...

//THIS DOESN'T COMPILE: aTest.DoIt<T>(i);


Have you tried

aTest.template DoIt<T>(i);


Yes! this compiles fine! Thanks!! Strange syntaxm, though.

Fernando Cuenca.
(mail to: fernando_cuenca *at* yahoo *dot* com *dot* ar)
Jul 22 '05 #4

P: n/a
Fernando Cuenca wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
"Fernando Cuenca" <no**************@yahoo.com> wrote...
//THIS DOESN'T COMPILE: aTest.DoIt<T>(i);


Have you tried

aTest.template DoIt<T>(i);

Yes! this compiles fine! Thanks!! Strange syntaxm, though.


While the code suggested by Victor is portable, it is also a bug in gcc
3.3.1 that forces you to need it. If I were you, I would upgrade the
compiler.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.