Archived

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

Member Template Specialization

This topic is 5821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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