I'm trying to create custom containers, and use those as return types in a template class. I'm having to use C 98, so wonder how to get the following to compile:
#include <string>
#include <vector>
/// Make a container called Vector<T>
template<typename T>
struct Vector
{
typedef std::vector<T> type;
};
/// Some ordinary class that holds that data.
class Data
{
public:
Data();
template<typename T>
typename Vector<T>::type* getVec();
/// Specialization
template<>
typename Vector<int>::type* getVec();
};
The error from the compiler is:
error: no function template matches function template specialization 'getVec'
Why does the 'int' specialization of getVec() not work?
CodePudding user response:
The C 98 rules about the placement of specialisations are somewhat less intuitive than modern C .
Suffice it to say, you need to place the specialisation at namespace scope
/// Specialization - declare after the class
template<>
Vector<int>::type* Data::getVec<int>();
Beyond that, you must specify the <int>
parameter explicitly since it's not deducible in the function's declaration. I also removed the redundant typename
(we are now in a context with concrete types).
CodePudding user response:
You might do
/// Some ordinary class that holds that data.
class Data
{
public:
Data();
template<typename T>
typename Vector<T>::type* getVec();
};
/// Specialization
template<>
Vector<int>::type* Data::getVec<int>()
{
return NULL;
}