A
Adam Nielsen
Hi everyone,
I've having some trouble trying to specialise a template. My original
code, with one typename, worked:
template <typename T>
class MyClass
{
private:
T data;
public:
const std::string& toString() const
{
// convert this->data to a string and return
}
};
const std::string& MyClass<std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}
However I then tried to extend the class with a second template
parameter, and it stopped working:
template <class C, typename T>
class MyClass
...
template <class C>
const std::string& MyClass<C, std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}
It would appear to work if I write code for every possible value of C,
but then that's the whole reason for having a template! I'm assuming
I've just gotten the syntax wrong, so if someone would be kind enough to
point out the correct way of doing this it would be much appreciated!
Many thanks,
Adam.
I've having some trouble trying to specialise a template. My original
code, with one typename, worked:
template <typename T>
class MyClass
{
private:
T data;
public:
const std::string& toString() const
{
// convert this->data to a string and return
}
};
const std::string& MyClass<std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}
However I then tried to extend the class with a second template
parameter, and it stopped working:
template <class C, typename T>
class MyClass
...
template <class C>
const std::string& MyClass<C, std::string>::toString() const
{
return this->data; // don't bother with unnecessary conversion
}
It would appear to work if I write code for every possible value of C,
but then that's the whole reason for having a template! I'm assuming
I've just gotten the syntax wrong, so if someone would be kind enough to
point out the correct way of doing this it would be much appreciated!
Many thanks,
Adam.