"Explicit specialization only allowed at file or namespace scope"

Discussion in 'C++' started by Bas, Jun 3, 2009.

  1. Bas

    Bas Guest

    Explicit specialization only allowed at file or namespace scope" .. is the
    error I got from a compiler. Another compiler doesn't
    complain.
    Who is right?

    Thanks on forehand,

    Bas

    #include <iostream>

    class Test
    {
    public:
    int x,y;

    Test(int a, int b) : x(a), y(b) {};

    template<typename x>
    x telop(x a, x b)
    {
    return a + b;
    }

    template<> // One doesn't like this one.
    int telop<int>(int a, int b)
    {
    return (a + b) * 10;
    }
    };


    int _tmain(int argc, _TCHAR* argv[])
    {
    Test t(3.14, 4.545);

    std::cout << t.telop<int>(3.14, 4.545);

    std::cin.get();
    return 0;
    }
     
    Bas, Jun 3, 2009
    #1
    1. Advertising

  2. * Bas:
    > Explicit specialization only allowed at file or namespace scope" .. is
    > the error I got from a compiler. Another compiler doesn't complain.
    > Who is right?
    >
    > Thanks on forehand,
    >
    > Bas
    >
    > #include <iostream>
    >
    > class Test
    > {
    > public:
    > int x,y;
    >
    > Test(int a, int b) : x(a), y(b) {};
    >
    > template<typename x>
    > x telop(x a, x b)
    > {
    > return a + b;
    > }
    >
    > template<> // One doesn't like this one.
    > int telop<int>(int a, int b)
    > {
    > return (a + b) * 10;
    > }
    > };


    Yeah, you have to move that specialization out of the class definition, to the
    namespace that the class is defined in, specified by §14.7.3/2.


    > int _tmain(int argc, _TCHAR* argv[])


    This is not a valid 'main'.


    > {
    > Test t(3.14, 4.545);
    >
    > std::cout << t.telop<int>(3.14, 4.545);
    >
    > std::cin.get();
    > return 0;
    > }



    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Jun 3, 2009
    #2
    1. Advertising

  3. Bas

    Neelesh Guest

    On Jun 3, 12:55 pm, "Bas" <> wrote:
    > Explicit specialization only allowed at file or namespace scope" .. is the
    > error I got from a compiler. Another compiler doesn't
    > complain.
    > Who is right?
    >
    > Thanks on forehand,
    >
    > Bas
    >
    > #include <iostream>
    >
    > class Test
    > {
    >    public:
    >    int x,y;
    >
    >    Test(int a, int b) : x(a), y(b) {};
    >
    >    template<typename x>
    >    x telop(x a, x b)
    >    {
    >       return a + b;
    >    }
    >
    >    template<>                                // One doesn't like this one.
    >    int telop<int>(int a, int b)
    >    {
    >       return (a + b) * 10;
    >    }
    > };


    You will need to give the specialization outside the class in the
    global / namespace scope.

    template<> int Test::telop<int>(int a , int b)
    {
    return (a+b)*10;
    }


    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {

    That's not std C++, a portable code needs to use int main and char*
    argv[])
     
    Neelesh, Jun 3, 2009
    #3
  4. Bas

    Bas Guest

    Thanks both of you.

    Bas

    "Neelesh" <> wrote in message
    news:...
    On Jun 3, 12:55 pm, "Bas" <> wrote:
    > Explicit specialization only allowed at file or namespace scope" .. is the
    > error I got from a compiler. Another compiler doesn't
    > complain.
    > Who is right?
    >
    > Thanks on forehand,
    >
    > Bas
    >
    > #include <iostream>
    >
    > class Test
    > {
    > public:
    > int x,y;
    >
    > Test(int a, int b) : x(a), y(b) {};
    >
    > template<typename x>
    > x telop(x a, x b)
    > {
    > return a + b;
    > }
    >
    > template<> // One doesn't like this one.
    > int telop<int>(int a, int b)
    > {
    > return (a + b) * 10;
    > }
    > };


    You will need to give the specialization outside the class in the
    global / namespace scope.

    template<> int Test::telop<int>(int a , int b)
    {
    return (a+b)*10;
    }


    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {

    That's not std C++, a portable code needs to use int main and char*
    argv[])
     
    Bas, Jun 3, 2009
    #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.

Share This Page