Specializing template member function

I

Igor R.

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.
 
V

Victor Bazarov

Igor said:
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
 
V

Vladimir Jovic

Victor said:
No.

Consider overloading.


Yes, this is non-standard.


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

Igor R.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top