2

我对模板有一个非常奇怪的问题。得到一个错误error: ‘traits’ is not a template。我无法在示例测试项目中重现该问题。但它发生在我的项目上(比我在这里发布的要大)。

无论如何,以下是我拥有的文件和用法。任何人都知道这个错误何时发生?

我有以下内容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;
    };
}

这在另一个头文件中使用。

namespace silc {

    template<typename T>
    class node {                
    public:

        typedef typename traits<T>::const_reference const_reference;

        const_reference value() const {
            /* ... */
        }
    }
}
4

1 回答 1

3

模板专业化的语法是......不愉快。

我相信您的错误可以通过替换来修复struct traits<U>struct traits但保持struct traits<U*>原样!)。

但看看光明的一面!至少您没有对函数类型进行部分专业化:

// 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*)> { ... };
于 2010-02-21T06:06:36.490 回答