'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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top