473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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)
Jul 22 '05 #1
4 1783
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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
by: James Kanze | last post by:
On May 16, 9:34 am, Paavo Helde <nob...@ebi.eewrote:
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.