Archived

This topic is now archived and is closed to further replies.

Shannon Barber

Member Template Specialization

Recommended Posts

Shannon Barber    1681
Is Member Template Specialization part of the C++ spec? I swear I just read in "Modern C++ Desing" that it isn''t - but MSVC will actually let you specialize member function templates! I can''t imagine the MS compiler having such an extention when they lack other template features.

Share this post


Link to post
Share on other sites
Void    126
Where did u read that? I though it was VC++ that doesn't allow member function specializations!

The below compiles fine on gcc, comeau but fails with VC++. What do u think?

    
#include <iostream>

using namespace std;

template <typename T>
class C
{
public:
template <typename X>
void Method( X x)
{
cout << "Generic called" << endl;
}
};

template <>
template<> void C<int>::Method<float>( float x )
{
cout << "Specialization called" << endl;
}


int main()
{
C<int> obj;

obj.Method( 1.2f ); // calls specialized

obj.Method( 1 ); // calls generic


return 0;
}

Share this post


Link to post
Share on other sites