Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicit template instantiation from template function doesn't compile?

Reply
Thread Tools

Explicit template instantiation from template function doesn't compile?

 
 
Fernando Cuenca
Guest
Posts: n/a
 
      09-05-2004
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)
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-05-2004
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-05-2004
"Fernando Cuenca" <(E-Mail Removed)> 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)



 
Reply With Quote
 
Fernando Cuenca
Guest
Posts: n/a
 
      09-05-2004
"Victor Bazarov" <(E-Mail Removed)> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
> "Fernando Cuenca" <(E-Mail Removed)> 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)
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      09-06-2004
Fernando Cuenca wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message news:<74J_c.124781$Fg5.23429@attbi_s53>...
>
>>"Fernando Cuenca" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of non-template member of template class Noah Roberts C++ 6 02-02-2011 09:53 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
?explicit instantiation member function of class template henkoo@gmail.com C++ 5 12-04-2005 04:06 PM
explicit instantiation of template methods of template classes Thomas Maier-Komor C++ 6 05-19-2005 08:00 AM
problem with explicit template instantiation in Visual C++ 6.0 . C. Carbonera C++ 4 02-05-2004 05:05 PM



Advertisments