473,698 Members | 2,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a template function from a template function

Hi all, I am trying to figure out what is causing the compile error in
the following example. I have two functions which are identical
(except one is templated), both trying to call a template function in
another class. The template function (func2) won't compile. I'm
hoping that I'm just missing something simple, but I can figure out
what it is.

struct A
{
template<typena me T>
void DoIt(T t)
{
t++;
}
};

struct B
{
void other()
{
func2<int>();
}

void func1()
{
A a;
int x = 0;
a.DoIt<int>(x);
}

template<typena me Y>
void func2()
{
A a;
Y x = 0;
a.DoIt<Y>(x); // FIXME: Compile error is here
}
};

The compile error is :
Test.cpp: In member function `void B::func2()':
Test.cpp:29: error: parse error before `;' token
where line 29 is the marked line.

Any help is greatly appreciated.

Sep 28 '07 #1
5 1601
On 28 Sep, 23:31, Jeff Newman <Jeffrey.M.New. ..@gmail.comwro te:
[snip]
The compile error is :
Test.cpp: In member function `void B::func2()':
Test.cpp:29: error: parse error before `;' token
where line 29 is the marked line.
Any help is greatly appreciated.
Comeau online accepts it OK - what compiler are
you using? If the answer is VC6 that's probably
your problem - search the archives of this group
for problems with this, esp w.r.t. templates.
Sep 28 '07 #2
Jeff Newman wrote:
Hi all, I am trying to figure out what is causing the compile error in
the following example. I have two functions which are identical
(except one is templated), both trying to call a template function in
another class. The template function (func2) won't compile. I'm
hoping that I'm just missing something simple, but I can figure out
what it is.

template<typena me Y>
void func2()
{
A a;
Y x = 0;
a.DoIt<Y>(x); // FIXME: Compile error is here
}
};

The compile error is :
Test.cpp: In member function `void B::func2()':
Test.cpp:29: error: parse error before `;' token
where line 29 is the marked line.

Any help is greatly appreciated.
Looks like a compiler bug.

--
Ian Collins.
Sep 28 '07 #3
On Sep 29, 12:31 am, Jeff Newman <Jeffrey.M.New. ..@gmail.comwro te:
Hi all, I am trying to figure out what is causing the compile error in
the following example. I have two functions which are identical
(except one is templated), both trying to call a template function in
another class. The template function (func2) won't compile. I'm
hoping that I'm just missing something simple, but I can figure out
what it is.
struct A
{
template<typena me T>
void DoIt(T t)
{
t++;
}
};
struct B
{
void other()
{
func2<int>();
}
void func1()
{
A a;
int x = 0;
a.DoIt<int>(x);
}
template<typena me Y>
void func2()
{
A a;
Y x = 0;
a.DoIt<Y>(x); // FIXME: Compile error is here
}
};
The compile error is :
Test.cpp: In member function `void B::func2()':
Test.cpp:29: error: parse error before `;' token
where line 29 is the marked line.
With what compiler? G++ (4.1.2) doesn't complain, but
explicitly specifying the template arguments to a function was
added to the standard fairly late, so it may not be supported by
older compilers. (E.g. I don't think VC++ 6.0 supports it,
although the more recent versions probably do.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 29 '07 #4
On 2007-09-28 19:41:44 -0400, tragomaskhalos
<da************ *@logicacmg.com said:
On 28 Sep, 23:31, Jeff Newman <Jeffrey.M.New. ..@gmail.comwro te:
[snip]
>The compile error is :
Test.cpp: In member function `void B::func2()':
Test.cpp:29: error: parse error before `;' token
where line 29 is the marked line.
Any help is greatly appreciated.

Comeau online accepts it OK - what compiler are
you using? If the answer is VC6 that's probably
your problem - search the archives of this group
for problems with this, esp w.r.t. templates.
The error message sounds like one from gcc. Search the archives of this
group for problems with this, esp w.r.t. templates.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 29 '07 #5
On Sep 29, 6:39 am, Pete Becker <p...@versatile coding.comwrote :
On 2007-09-28 19:41:44 -0400, tragomaskhalos
<dave.du.verg.. .@logicacmg.com said:
On 28 Sep, 23:31,JeffNewma n<Jeffrey.M.New ...@gmail.comwr ote:
[snip]
The compile error is :
Test.cpp: In member function `void B::func2()':
Test.cpp:29: error: parse error before `;' token
where line 29 is the marked line.
Any help is greatly appreciated.
Comeau online accepts it OK - what compiler are
you using? If the answer is VC6 that's probably
your problem - search the archives of this group
for problems with this, esp w.r.t. templates.

The error message sounds like one from gcc. Search the archives of this
group for problems with this, esp w.r.t. templates.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Thanks for the help. The compiler was GCC 3.3, and it does appear to
be a bug in it. In case anyone else is searching for this issue, a
workaround is to take out the explicit type usage in func2 and just
allow the compiler figure out which version to call:

template<typena me Y>
void func2()
{
A a;
Y x = 0;
a.DoIt(x); // NOTE: This now compiles
}

Oct 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5074
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
3
1377
by: ogtindeed | last post by:
Hi all, I need some help. I am trying to instantiate a Template and call a member function, but have not been able - Now this is what I am trying to do: foo.h =========
31
3502
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
10
1783
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles into an object file but then at the final stage it says that it can't find template function. The platform is WindowsXP Pro, MSCV++ ..Net. More specifically what I want to do is write an atl algorithm that will also
2
6191
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the general function template... template <class T>
5
1680
by: shuisheng | last post by:
Dear All, Assume I have two templates as follows: //! Add #1: p3 = p1 + p2. template<class _T1, class _T2, class _T3> void Add(const _T1 *p1, const _T2 *p2, _T3 *p3, size_t n) { for (size_t i = 0; i < n; ++i) p3 = p1 + p2; }
4
4056
by: James Aguilar | last post by:
Guys, When I specialize a template class member function (I.e. a member function of a template class) based on that class' type, bad things happen. Here's some code: ---- test_header.h #ifndef TEST_HEADER_H #define TEST_HEADER_H
5
3179
by: antani | last post by:
I need to implement a function with a argument that is a compare function. This compare function must be several for every necessity. For example , I would like a compare function to analyze element of list that are even or a compare function to analyze odd element. example: void f (compare )
7
1926
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial specialization). template <typename TOut, typename TIn> void InverseRescaleFunction(TOut *out, const TIn *in, double intercept, double slope, size_t size) { ... }
7
236
by: sheffmail | last post by:
I have the following code: template <class T> struct MyS { }; template<class T> inline const T& function(const std::locale& loc) {
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.