Explicit template instantiation from template function doesn't compile?

  • Thread starter Fernando Cuenca
  • Start date
F

Fernando Cuenca

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)
 
G

Gianni Mariani

Fernando said:
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.
 
V

Victor Bazarov

Fernando Cuenca said:
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);

??
 
F

Fernando Cuenca

Victor Bazarov said:
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)
 
G

Gianni Mariani

Fernando said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top