static template functions inside classes

Discussion in 'C++' started by Patrick Kowalzick, Oct 12, 2004.

  1. 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();
    }
    Patrick Kowalzick, Oct 12, 2004
    #1
    1. Advertising

  2. Patrick Kowalzick

    Sumit Rajan Guest

    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>
    Sumit Rajan, Oct 12, 2004
    #2
    1. Advertising

  3. Patrick Kowalzick wrote in news:ckgf3a$2on$ 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/womble/c /template-faq.html

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Oct 12, 2004
    #3
  4. Thx Rob & Sumit,

    perfect. Didn't knew this before.

    Patrick
    Patrick Kowalzick, Oct 12, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. claude uq
    Replies:
    3
    Views:
    497
    tom_usenet
    Dec 18, 2003
  2. Replies:
    2
    Views:
    371
    Kai-Uwe Bux
    Feb 5, 2006
  3. Hicham Mouline
    Replies:
    0
    Views:
    424
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    406
    Michael DOUBEZ
    Apr 24, 2009
  5. Mike Stephens

    Do you nest classes inside classes?

    Mike Stephens, May 25, 2009, in forum: Ruby
    Replies:
    10
    Views:
    213
    Robert Klemme
    May 27, 2009
Loading...

Share This Page