Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static template functions inside classes

Reply
Thread Tools

static template functions inside classes

 
 
Patrick Kowalzick
Guest
Posts: n/a
 
      10-12-2004
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();
}


 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      10-12-2004
Patrick Kowalzick wrote:
> 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.
--
Sumit Rajan <sumit DOT rajan AT gmail DOT com>
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      10-12-2004
Patrick Kowalzick wrote in news:ckgf3a$2on$(E-Mail Removed) 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
T::template foo<int>(); // not compiling


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


Here's a template FAQ:

http://www.decadentplace.org.uk/womb...plate-faq.html

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Patrick Kowalzick
Guest
Posts: n/a
 
      10-12-2004
Thx Rob & Sumit,

perfect. Didn't knew this before.

Patrick


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Template Member Functions of Template Classes gogo1969@gmail.com C++ 2 02-05-2006 03:29 AM
Modifiers applied to attributes, local variables, member functions, classes and inncer classes ! Razvan Java 11 07-17-2004 08:57 PM
template functions calls within non template classes. How to do it? claude uq C++ 3 12-18-2003 04:55 PM



Advertisments