DEVELOP > c-Plus-Plus > Explicit template instantiation from template function doesn't compile?
| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Fernando Cuenca" |
| Date: |
05 Sep 2004 12:24:59 PM |
| Object: |
Explicit template instantiation from template function doesn't compile? |
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)
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Explicit template instantiation from template function doesn't compile? |
05 Sep 2004 01:40:03 PM |
|
|
"Fernando Cuenca" <nomemandenbasura@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)
.
|
|
|
| User: "Fernando Cuenca" |
|
| Title: Re: Explicit template instantiation from template function doesn't compile? |
05 Sep 2004 06:41:49 PM |
|
|
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
"Fernando Cuenca" <nomemandenbasura@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)
.
|
|
|
| User: "Gianni Mariani" |
|
| Title: Re: Explicit template instantiation from template function doesn'tcompile? |
06 Sep 2004 11:06:11 AM |
|
|
Fernando Cuenca wrote:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
"Fernando Cuenca" <nomemandenbasura@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.
.
|
|
|
|
|
|
| User: "Gianni Mariani" |
|
| Title: Re: Explicit template instantiation from template function doesn'tcompile? |
05 Sep 2004 12:28:11 PM |
|
|
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.
.
|
|
|
|

|
Related Articles |
|
|