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) 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.
"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)
"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)
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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>
|
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...
|
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...
|
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>
|
by: yuanhp_china |
last post by:
I define a class in A.h:
template <class Tclass A{
public:
void get_elem( const T&) ;
};
|
by: James Kanze |
last post by:
On May 16, 9:34 am, Paavo Helde <nob...@ebi.eewrote:
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |