static template functions inside classes

P

Patrick Kowalzick

Dear all,

the code below does not compile with g++ (3.3.1), but with MSVC (7.1). So I
am wondering what might be wrong with it. Any ideas?

Thanks,
Patrick


struct A
{
template <typename T> static void foo(T = T()) {}
};

template <typename T>
struct B
{
void foo()
{
T::foo( 0 ); // works
T::foo<int>( 0 ); // not compiling
T::foo<int>(); // not compiling
}
};

int main()
{
B<A> x;
x.foo();
}
 
S

Sumit Rajan

Patrick said:
Dear all,

the code below does not compile with g++ (3.3.1), but with MSVC (7.1). So I
am wondering what might be wrong with it. Any ideas?

Thanks,
Patrick


struct A
{
template <typename T> static void foo(T = T()) {}
};

template <typename T>
struct B
{
void foo()
{
T::foo( 0 ); // works
T::foo<int>( 0 ); // not compiling
T::foo<int>(); // not compiling

Look up the ".template" construct.

T::foo( 0 );
T::template foo<int>( 0 );
T::template foo<int>();

Regards,
Sumit.
 
R

Rob Williscroft

Patrick Kowalzick wrote in in
comp.lang.c++:
Dear all,

the code below does not compile with g++ (3.3.1), but with MSVC (7.1).
So I am wondering what might be wrong with it. Any ideas?

Thanks,
Patrick


struct A
{
template <typename T> static void foo(T = T()) {}
};

template <typename T>
struct B
{
void foo()
{
T::foo( 0 ); // works
T::foo<int>( 0 ); // not compiling
T::foo<int>(); // not compiling

You need to tell a conforming compiler that the dependent name "foo"
is a template:

T::template foo<int>( 0 ); // not compiling
}
};

int main()
{
B<A> x;
x.foo();
}

Here's a template FAQ:

http://www.decadentplace.org.uk/womble/c++/template-faq.html

Rob.
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top