g++ vs msvc++

Discussion in 'C++' started by rbfish@hotmail.com, Jan 31, 2006.

  1. Guest

    Anyone can tell me g++ can NOT compile the simple program with error:
    test.cpp:12: error: invalid explicit specialization before '>' token
    test.cpp:12: error: explicit specialization in non-namespace scope
    `class C'
    test.cpp:14: error: invalid member function declaration

    But MSVC++ can.

    Thanks
    rbfish

    #include "stdio.h"

    class C
    {
    public:
    template<int N>
    void f()
    {
    printf("f<N=%d>()\n", N);
    }

    template<>
    void f<1>()
    {
    printf("f<1>()\n");
    }
    };

    int main()
    {
    C c;
    c.f<1>();
    c.f<2>();

    return 0;
    }
    , Jan 31, 2006
    #1
    1. Advertising

  2. wrote:
    > #include "stdio.h"
    >
    > class C
    > {
    > public:
    > template<int N>
    > void f()
    > {
    > printf("f<N=%d>()\n", N);
    > }
    >
    > template<>
    > void f<1>()
    > {
    > printf("f<1>()\n");
    > }
    > };
    >
    > int main()
    > {
    > C c;
    > c.f<1>();
    > c.f<2>();
    >
    > return 0;
    > }


    > Anyone can tell me g++ can NOT compile the simple program with error:
    > test.cpp:12: error: invalid explicit specialization before '>' token
    > test.cpp:12: error: explicit specialization in non-namespace scope
    > `class C'
    > test.cpp:14: error: invalid member function declaration
    >
    > But MSVC++ can.


    Because gcc is correct and Visual C++ is wrong.

    The error says it all. You can't have an explicit specialization in a
    class declaration. The explicit specialization must occur at namespace
    scope:

    template<>
    void C::f<1>()
    {
    printf("f<1>()\n");
    }

    Best regards,

    Tom
    Thomas Tutone, Jan 31, 2006
    #2
    1. Advertising

  3. eiji Guest

    > c.f<1>();
    > c.f<2>();


    You try to use the template -typename as argument!
    This should not be possible!

    C c;
    c.f<int>(1);
    c.f<double>(2);
    print 1 as 1 and 2 as 2.0
    eiji, Jan 31, 2006
    #3
  4. eiji Guest

    Okay, I was wrong. I should grap a templates-book on
    expression-templates /metaprogramming/etc.
    Sorry
    eiji, Jan 31, 2006
    #4
  5. Guest

    Thank you very much.
    , Jan 31, 2006
    #5
    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. Dan
    Replies:
    0
    Views:
    1,090
  2. Victor Bazarov

    Re: array of complex numbers in MSVC++ 6

    Victor Bazarov, Jul 5, 2003, in forum: C++
    Replies:
    0
    Views:
    477
    Victor Bazarov
    Jul 5, 2003
  3. Arkadiy Vertleyb
    Replies:
    1
    Views:
    1,429
    Victor Bazarov
    Jul 9, 2003
  4. Trent WADDINGTON

    MSVC 4.2 needed

    Trent WADDINGTON, Sep 29, 2003, in forum: C++
    Replies:
    4
    Views:
    534
    Jack Klein
    Sep 30, 2003
  5. Rodolphe
    Replies:
    4
    Views:
    1,060
    Rodolphe
    Oct 3, 2003
Loading...

Share This Page