views:

170

answers:

1

I am having a very weird issue with templates. Getting an error error: ‘traits’ is not a template. I couldn't reproduce the issue on a sample test project. But it happens on my project (which is bigger than I can post here).

Anyway, following are the files and usages I have. Anyone have any idea about when this error occurs?

I have the following in traits.hpp.

namespace silc 
{
    template<class U>
    struct traits<U>
    {
        typedef const U& const_reference;
    };

    template<class U>
    struct traits<U*>
    {
        typedef const U* const_reference;
    };
}

This is used in another header file.

namespace silc {

    template<typename T>
    class node {                
    public:

        typedef typename traits<T>::const_reference const_reference;

        const_reference value() const {
            /* ... */
        }
    }
}
+1  A: 

Syntax for template specialization is... not pleasant.

I believe your error can be fixed by replacing struct traits<U> by struct traits (but leave struct traits<U*> as-is!).

But look on the bright side! At least you aren't doing partial specialization over function types:

// Partial class specialization for
// function pointers of one parameter and any return type
template <typename T, typename RetVal>
class del_ptr<T, RetVal (*)(T*)> { ... };

// Partial class specialization for
// functions of one parameter and any return type
template <typename T, typename RetVal>
class del_ptr<T, RetVal(T*)> { ... };

// Partial class specialization for
// references to functions of one parameter and any return type
template <typename T, typename RetVal>
class del_ptr<T, RetVal(&)(T*)> { ... };
Ben Karel
Thanks. That was a stupid mistake I have ever done. Thanks again for pointing that out.
Appu