Calling a template function from a template function

J

Jeff Newman

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<typename T>
void DoIt(T t)
{
t++;
}
};

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

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

template<typename 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.
 
T

tragomaskhalos

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.
 
I

Ian Collins

Jeff said:
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<typename 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.
 
J

James Kanze

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<typename T>
void DoIt(T t)
{
t++;
}
};
struct B
{
void other()
{
func2<int>();
}
void func1()
{
A a;
int x = 0;
a.DoIt<int>(x);
}
template<typename 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.)
 
P

Pete Becker

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.
 
J

Jeff Newman

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<typename Y>
void func2()
{
A a;
Y x = 0;
a.DoIt(x); // NOTE: This now compiles
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top