对不起,这个非常模糊的标题,很难描述。
我遇到的错误是这个,我不知道这意味着什么:
carray.h:176: 错误: 'typename Carray<T, Allocator>::is_iterator' 名称 'template<class T, class Allocator> template<class I, bool check> struct Carray<T, Allocator>::is_iterator',这不是一种类型
我有这个片段来检测某些东西是否是迭代器并使用正确的重载(http://stackoverflow.com/questions/6050441/why-does-this-constructor-overload-resolve-incorrectly)。这编译:
template<class T> class Carray {
private:
// uses SFINAE to determine if the passed in type is indeed an iterator
template<class It>
class is_iterator_impl {
private:
typedef char yes[1];
typedef char no[2];
template<class C>
static yes& _test(typename C::iterator_category*);
template<class>
static no& _test(...);
public:
static const bool value = sizeof(_test<It>(0)) == sizeof(yes);
};
template<class It, bool check = is_iterator_impl<It>::value> struct is_iterator { typedef void type; };
template<class It> struct is_iterator<It*, false> { typedef void type; };
template<class It> struct is_iterator<It, false> { };
public:
template<class It>
Carray(It first, It last, typename is_iterator<It>::type *dummy = 0) {
// create array from 2 iterators
}
};
现在我想将实现与声明分开,我尝试了这个,但我得到了错误:
template<class T> class Carray {
private:
// uses SFINAE to determine if the passed in type is indeed an iterator
template<class It> class is_iterator_impl;
template<class It, bool check = is_iterator_impl<It>::value> struct is_iterator { typedef void type; };
template<class It> struct is_iterator<It*, false> { typedef void type; };
template<class It> struct is_iterator<It, false> { };
public:
template<class It> Carray(It first, It last, typename is_iterator<It>::type *dummy = 0);
};
template<class T>
template<class It>
Carray<T>::Carray(It first, It last, typename Carray<T>::is_iterator<It>::type *dummy) {
// create array from 2 iterators - ERROR IN THIS DEFINITION
}
template<class T>
template<class It>
class Carray<T>::is_iterator_impl {
private:
typedef char yes[1];
typedef char no[2];
template<class C>
static yes& _test(typename C::iterator_category*);
template<class>
static no& _test(...);
public:
static const bool value = sizeof(_test<It>(0)) == sizeof(yes);
};
我正在使用 g++ 4.5.5。