This question is related to my last one. I am trying to solve the problem using traits<T>
and traits<T*>
. Please consider the following code.
template<typename T>
struct traits
{
typedef const T& const_reference;
};
template<typename T>
struct traits<T*>
{
typedef const T const_reference;
};
template<typename T>
class test
{
public:
typedef typename traits<T>::const_reference const_reference;
test() {}
const_reference value() const {
return f;
}
private:
T f;
};
int main()
{
const test<foo*> t;
const foo* f = t.value(); // error here. cannot convert ‘const foo’ to ‘const foo*’ in initialization
return 0;
}
So it looks like compiler is not considering the traits specialization for pointers and taking return type of value()
as const foo
rather than const foo*
. What am I doing wrong here?
Any help would be great!