繁体   English   中英

部分特化中未使用的模板参数

[英]Template parameters not used in partial specialization

我有以下代码:

template<typename T, typename Allocator = std::allocator<T> >
class Carray {
    // ...
    typedef T* pointer;
    typedef pointer iterator;
    // ...
};

现在我正在尝试为iterator_traits做部分特化。 对我来说似乎没问题,但g ++ 4.4.5抱怨:

#include <iterator>

namespace std {
    template<typename T, typename Allocator>
    struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128
        typedef T value_type;
        typedef typename Allocator::difference_type difference_type;
        typedef typename Allocator::reference reference;
        typedef typename Allocator::pointer pointer;
        typedef typename std::random_access_iterator_tag iterator_category;
    };
}

这是完整的错误消息:

carray.h:128: error: template parameters not used in partial specialization:
carray.h:128: error:         ‘T’
carray.h:130: error: ‘Allocator’ has not been declared
carray.h:131: error: ‘Allocator’ has not been declared
carray.h:132: error: ‘Allocator’ has not been declared

你根本不需要专门化: iterator_traits已经专门用于指针类型,如果你最终得到一个类类型的迭代器,你可以在迭代器类中定义那些必需的typedef

问题是为了匹配主要特化,编译器需要获取使用模板的参数,将它们插入特化,并查看它们是否匹配。

考虑以下简化方案中会发生什么:

template <typename T> struct S { typedef int type; };

template <typename T> 
struct Traits { };

template <typename T> 
struct Traits<typename S<T>::type> { };

编译器应该如何知道插入S T是什么,或者某些S<T>::type是否真的意味着只是int

问题是嵌套的typedef( ::type )取决于模板参数( T )。 如果是函数参数列表或部分特化中的情况,则无法推导出类型T (它是“非推导的上下文”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM