Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Template function calling a templated function in the templated class

57 views
Skip to first unread message

Charles Karney

unread,
Oct 9, 2006, 11:26:03 AM10/9/06
to bug-gp...@gnu.org
Configuration:
g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
Fedora Core 4, 2.6.16, i686

Consider the following code

#include <iostream>
#include <limits>

class foo {
public:
template<class S> int foof0()
{ return std::numeric_limits<S>::digits; }
template<class S> int foof1(S a)
{ return std::numeric_limits<S>::digits; }
};

class bar {
public:
template<class X> int barf(X x) { return x.foof0<double>(); }
// template<class X> int barf(X x) { return x.foof1(1.0); }
};

int main() {
foo a;
bar b;
std::cout << b.barf(a) << std::endl;
}

bar::barf is templated on the type of its argument and, with an argument
of type foo, is supposed to call foo::foof0<double>(). Compiling this
with g++ gives:

g++ test.cpp -o test
test.cpp: In member function 'int bar::barf(X)':
test.cpp:14: error: expected primary-expression before 'double'
test.cpp:14: error: expected ';' before 'double'
test.cpp:14: error: expected unqualified-id before '>' token

Notes:

(1) Replacing the definition of bar::barf by the commented version makes
the bug go away (the program prints out 53).

(2) The problem evidently isn't with the instantiation of bar::barf. I
get precisely the same error with an empty main program.

(3) Visual C++ 7.0 compiles this without complaint, so I don't think I'm
misusing C++.

Thanks for your attention...

--
Charles Karney <cka...@sarnoff.com>
Sarnoff Corporation, Princeton, NJ 08543-5300

URL: http://charles.karney.info
Tel: +1 609 734 2312
Fax: +1 609 734 2662


Charles Karney

unread,
Oct 9, 2006, 1:39:08 PM10/9/06
to Thomas Maeder, bug-gp...@gnu.org
Thomas Maeder wrote:

> "Charles Karney" <cka...@sarnoff.com> writes:
>
>
>> class bar {
>> public:
>> template<class X> int barf(X x) { return x.foof0<double>(); }
>
>
> foof0 is a dependent name naming a member template; such names have to
> prefixed with the keyword template:

>
> template<class X>
> int barf(X x)
> {
> return x.template foof0<double>();
> }

Thanks! I should have figured that out myself...

0 new messages