g++ vs msvc++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rbfish@hotmail.com

    #1

    g++ vs msvc++

    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;
    }

  • Thomas Tutone

    #2
    Re: g++ vs msvc++

    rbfish@hotmail. com wrote:[color=blue]
    > #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;
    > }[/color]
    [color=blue]
    > 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.[/color]

    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

    Comment

    • eiji

      #3
      Re: g++ vs msvc++

      > c.f<1>();[color=blue]
      > c.f<2>();[/color]

      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

      Comment

      • eiji

        #4
        Re: g++ vs msvc++

        Okay, I was wrong. I should grap a templates-book on
        expression-templates /metaprogramming/etc.
        Sorry

        Comment

        • rbfish@hotmail.com

          #5
          Re: g++ vs msvc++

          Thank you very much.

          Comment

          Working...