我正在尝试按照Dr.Dobb's概述的方法为自定义模板化容器类实现迭代器。它工作正常,我试图为嵌套的迭代器定义迭代器成员函数
template <typename T>
class Container
{
template <bool isConst = false>
class Iterator
{
Iterator& operator++();
Iterator operator++(int);
};
};
这样的课外:
template <typename T>
template <bool isConst>
//Container<T>::Iterator<isConst>& // g++ 4.8.1 is fine with that
typename Container<T>::Iterator<isConst>& // VS 2012 is fine with that
//typename Container<T>::template Iterator<isConst>& // again fine with g++
Container<T>::Iterator<isConst>::operator++()
{
++index_;
return *this;
}
template <typename T>
template <bool isConst>
//Container<T>::Iterator<isConst> // g++ 4.8.1 is fine with that
typename Container<T>::Iterator<isConst> // VS 2012 is fine with that
//typename Container<T>::template Iterator<isConst> // again fine with g++
Container<T>::Iterator<isConst>::operator++(int)
{
auto tmp(*this);
this->operator++();
return tmp;
}
到目前为止,我已经能够识别出三种不同的变体(下面详细说明),它们可以与g ++ 4.8.1(带有C ++ 0x)或Visual Studio 2012(Update 4)一起使用,但不能用于两者同时进行。
这样做的正确方法是什么?
从2009年开始有点related question,但据我所知,它不包含 Variant 4 ,我认为现在修复编译器错误。 ;)
还有this question,这让我觉得 Variant 3 应该是那个,并且g ++对 Variant 1 的接受是礼貌的
我在底部包含了完整的精简测试应用程序;它应该按照提供的方式进行编译(用相应的行注释)。
Container<T>::Iterator<isConst> // g++ 4.8.1 is fine with that
对于此变体,VS会抱怨从属名称不是类型,并建议使用 typename 关键字:
2>..\demo\demo.cpp(70): warning C4346: 'Container<T>::?$Iterator@$Q_N(*(_CAAB@))' : dependent name is not a type
2> prefix with 'typename' to indicate a type
2>..\demo\demo.cpp(70): error C2143: syntax error : missing ';' before '&'
2>..\demo\demo.cpp(70): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>..\demo\demo.cpp(70): error C2936: 'Container<T>::Iterator<isConst>' : template-class-id redefined as a global data variable
2>..\demo\demo.cpp(70): fatal error C1903: unable to recover from previous error(s); stopping compilation
typename Container<T>::Iterator<isConst>& // VS 2012 is fine with that
如果仅使用 typename 关键字(如VS for Variant 1所示),g ++对此并不满意:
main.cpp:71:28: error: non-template 'Iterator' used as template
typename Container<T>::Iterator<isConst>& // VS 2012 is fine with that
^
main.cpp:71:28: note: use 'Container<T>::template Iterator' to indicate that it is a template
main.cpp:71:28: error: expected unqualified-id at end of input
typename Container<T>::template Iterator<isConst>& // again fine with g++
如果使用了两个关键字(如G ++ for Variant 2所示),VS再次抱怨:
2>..\demo\demo.cpp(78): error C2244: 'Container<T>::Iterator<isConst>::operator ++' : unable to match function definition to an existing declaration
2> definition
2> 'Container<T>::Iterator<isConst> &Container<T>::Iterator<isConst>::operator ++(void)'
2> existing declarations
2> 'Container<T>::Iterator<isConst> Container<T>::Iterator<isConst>::operator ++(int)'
2> 'Container<T>::Iterator<isConst> &Container<T>::Iterator<isConst>::operator ++(void)'
2>..\demo\demo.cpp(92): error C2244: 'Container<T>::Iterator<isConst>::operator ++' : unable to match function definition to an existing declaration
2> definition
2> 'Container<T>::Iterator<isConst> Container<T>::Iterator<isConst>::operator ++(int)'
2> existing declarations
2> 'Container<T>::Iterator<isConst> Container<T>::Iterator<isConst>::operator ++(int)'
2> 'Container<T>::Iterator<isConst> &Container<T>::Iterator<isConst>::operator ++(void)'
由于自动关键字,g ++需要 -std = c ++ 0x 。
template <bool isConst, typename IsTrue, typename IsFalse>
struct Choose;
template <typename IsTrue, typename IsFalse>
struct Choose<true, IsTrue, IsFalse>
{
typedef IsTrue type;
};
template <typename IsTrue, typename IsFalse>
struct Choose<false, IsTrue, IsFalse>
{
typedef IsFalse type;
};
template <typename T>
class Container
{
public:
template <bool isConst = false>
class Iterator
{
public:
typedef typename Choose<isConst, T const&, T&>::type reference;
typedef typename Choose<isConst, T const*, T*>::type pointer;
typedef typename Choose<isConst,
Container<T> const*,
Container<T>*>::type ObjectPointer;
friend class Iterator<true>;
Iterator(
ObjectPointer object = 0,
unsigned long long const index = 0);
Iterator(Iterator<false> const& other);
Iterator& operator++();
Iterator operator++(int);
private:
ObjectPointer object_;
unsigned long long index_;
};
typedef Iterator<> iterator;
typedef Iterator<true> const_iterator;
};
template <typename T>
template <bool isConst>
Container<T>::Iterator<isConst>::Iterator(
ObjectPointer object,
unsigned long long const index)
: object_(object)
, index_ (index )
{}
template <typename T>
template <bool isConst>
Container<T>::Iterator<isConst>::Iterator(Iterator<false> const& other)
: object_(other.object_)
, index_ (other.index_ )
{}
template <typename T>
template <bool isConst>
//Container<T>::Iterator<isConst>& // g++ 4.8.1 is fine with that
typename Container<T>::Iterator<isConst>& // VS 2012 is fine with that
//typename Container<T>::template Iterator<isConst>& // again fine with g++
Container<T>::Iterator<isConst>::operator++()
{
++index_;
return *this;
}
template <typename T>
template <bool isConst>
//Container<T>::Iterator<isConst> // g++ 4.8.1 is fine with that
typename Container<T>::Iterator<isConst> // VS 2012 is fine with that
//typename Container<T>::template Iterator<isConst> // again fine with g++
Container<T>::Iterator<isConst>::operator++(int)
{
auto tmp(*this);
this->operator++();
return tmp;
}
int main(int, char*[])
{
Container<int> c;
Container<int>::iterator it(&c, 0);
Container<int>::const_iterator cIt = it;
Container<int>::const_iterator cIt0 = ++cIt;
Container<int>::const_iterator cIt1 = cIt++;
return 0;
}