Specializing template member function

Discussion in 'C++' started by Igor R., Nov 10, 2009.

  1. Igor R.

    Igor R. Guest

    Is it permitted in Standard to specialize template member function of
    a non-specialized class template? If yes, what's the correct syntax?

    template<class T> struct Test
    {
    template<class U> void f()
    {}
    // MSVC permits specializations inside the class scope, but it's
    seems to be non-standard:
    template<> void f<int>()
    {}
    };

    // doesn't compile both with MSVC and gcc:
    template<class T>
    template<>
    Test::f<char>()
    {}

    Thanks.
    Igor R., Nov 10, 2009
    #1
    1. Advertising

  2. Igor R.

    Igor R. Guest

    > // doesn't compile both with MSVC and gcc:
    > template<class T>
    > template<>
    > Test::f<char>()
    > {}


    I mean:
    template<class T>
    template<>
    Test<T>::f<char>()
    {}
    Igor R., Nov 10, 2009
    #2
    1. Advertising

  3. Igor R. wrote:
    > Is it permitted in Standard to specialize template member function of
    > a non-specialized class template? If yes, what's the correct syntax?


    No.

    Consider overloading.

    > template<class T> struct Test
    > {
    > template<class U> void f()
    > {}
    > // MSVC permits specializations inside the class scope, but it's
    > seems to be non-standard:
    > template<> void f<int>()
    > {}


    Yes, this is non-standard.

    > };
    >
    > // doesn't compile both with MSVC and gcc:
    > template<class T>
    > template<>
    > Test::f<char>()
    > {}
    >
    > Thanks.


    Sometimes it's possible to overload the function, but you need arguments
    to differentiate it from the template. You might give it a fake one...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 10, 2009
    #3
  4. Victor Bazarov wrote:
    > Igor R. wrote:
    >> Is it permitted in Standard to specialize template member function of
    >> a non-specialized class template? If yes, what's the correct syntax?

    >
    > No.
    >
    > Consider overloading.
    >
    >> template<class T> struct Test
    >> {
    >> template<class U> void f()
    >> {}
    >> // MSVC permits specializations inside the class scope, but it's
    >> seems to be non-standard:
    >> template<> void f<int>()
    >> {}

    >
    > Yes, this is non-standard.
    >
    >> };
    >>
    >> // doesn't compile both with MSVC and gcc:
    >> template<class T>
    >> template<>
    >> Test::f<char>()
    >> {}
    >>
    >> Thanks.

    >
    > Sometimes it's possible to overload the function, but you need arguments
    > to differentiate it from the template. You might give it a fake one...
    >


    He probably meant something like this:

    #include <iostream>
    template< typename T >
    class A
    {
    public:
    A();
    };
    template < typename T >
    A< T >::A()
    {
    std::cout<<"T"<<std::endl;
    }
    template <>
    A< int >::A()
    {
    std::cout<<"int"<<std::endl;
    }
    int main()
    {
    A< int > a1;
    A< double > a2;
    }



    --
    Bolje je ziveti sto godina kao bogatun, nego jedan dan kao siromah!
    Vladimir Jovic, Nov 11, 2009
    #4
  5. Igor R.

    Igor R. Guest

    Victor Bazarov,

    > Sometimes it's possible to overload the function, but you need arguments to differentiate it from the template. You might give it a fake one...


    Ok, I'll go this way. Thanks!


    Vladimir Jovic,

    > He probably meant something like this:


    No, I didn't.
    Igor R., Nov 11, 2009
    #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. Jeff
    Replies:
    2
    Views:
    410
  2. Simon G Best
    Replies:
    2
    Views:
    550
    Simon G Best
    Dec 29, 2006
  3. Rahul
    Replies:
    8
    Views:
    384
    Lionel B
    Jul 16, 2007
  4. John
    Replies:
    3
    Views:
    326
    James Kanze
    Aug 17, 2010
  5. m0shbear
    Replies:
    2
    Views:
    336
Loading...

Share This Page