'function' : cannot be explicitly specialized error

A

Aarti

Say I have a class template as follows

template<typename T>
class foo
{
public:
void test();
};

template<typename T>
void foo<T>::test()
{
// function implementation
}

Now if i want to specialize it for say strings, I would do it as
follows
template<>
class foo<string>
{
public:
void test();
};

template<> //Gives a error "function cannot be specialized"
void foo<string>::test()
{
// function implementation
}

My question is why do I need to omit the "template<> " for functions
of specialized template class? I am trying this with Visual C++
Express 2005 edition.
 
R

Robert Bauck Hamar

Aarti said:
Say I have a class template as follows

template<typename T>
class foo
{
public:
void test();
};

template<typename T>
void foo<T>::test()
{
// function implementation
}

Now if i want to specialize it for say strings, I would do it as
follows
template<>
class foo<string>
{
public:
void test();
};

template<> //Gives a error "function cannot be specialized"
void foo<string>::test()
{
// function implementation
}

My question is why do I need to omit the "template<> " for functions
of specialized template class? I am trying this with Visual C++
Express 2005 edition.

Because the standard says so. Note that foo<string>::test (assuming string
is a type) is not a template. It is an ordinary member function of the
specialised template foo.
 
A

Aarti

Because the standard says so. Note that foo<string>::test (assuming string
is a type) is not a template. It is an ordinary member function of the
specialised template foo.

Ok Got it. Thanks!!!
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top