避免通用容器中 begin() 和 end() 函数的重复代码
Avoiding duplicate code for begin() and end() functions in generic containers
有几个
类似的线程Q1或Q2与此线程相似,但不完全是。
问题只是以优雅的方式编写以下代码,没有代码重复:
template <typename T, class Container = std::vector<T> >
class container{
iterator begin(){
return iterator(data_.begin(), 1);
}
const_iterator begin() const{
return const_iterator(data_.begin(), 1);
}
Container data_;
};
Q2 有一种优雅的方法,可以避免具有完全相同签名的 const 和非 const 函数的代码重复。然而,这里的情况并非如此,因为没有将const_iterator转换为迭代器的转换。
Q1 提供了一种使用模板的好方法,但是,一旦容器是一个类,begin_impl
函数就会变得static
,同时friend
。
我想出的最优雅的代码是:
template <T>
class container{
template< typename I, typename C >
static I begin_impl( C & c ){
return I(data_.begin(), 1);
}
template< typename I, typename C >
friend I container<T>::begin_impl( C & c );
iterator begin(){
return container<T>::template begin_impl< iterator >( *this );
}
const_iterator begin() const{
return container<T>::template begin_impl< const_iterator >( *this );
}
}
我的问题是,在您看来,这是否是最优雅的方式。如果没有,请建议一些更好的代码。
编辑:我的迭代器实现:
class container{
template <bool isConst>
class iterator_ {
public:
typedef Container container_type;
typedef typename Container::value_type value_type;
typedef typename Container::difference_type difference_type;
typedef typename Container::size_type size_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
typedef typename Container::pointer pointer;
typedef typename Container::const_pointer const_pointer;
typedef typename std::forward_iterator_tag iterator_category;
template<bool isCond, typename cref, typename ref>
struct IS_CONST_REF{
typedef ref reference_type;
};
template<typename cref, typename ref>
struct IS_CONST_REF<true, cref, ref>{
typedef cref reference_type;
};
typedef typename IS_CONST_REF<isConst, const_reference, reference>::reference_type ref;
template<bool isCond, typename citr, typename itr>
struct IS_CONST_ITR{
typedef itr iterator_type;
};
template<typename citr, typename itr>
struct IS_CONST_ITR<true, citr, itr>{
typedef citr iterator_type;
};
typedef typename IS_CONST_ITR<isConst, typename Container::const_iterator, typename Container::iterator>::iterator_type itr;
iterator_()
:data_(), stepSize_(0){
}
iterator_(itr data, difference_type stepSize)
:data_(data), stepSize_(stepSize){
}
iterator_(const iterator_<false>& src)
:data_(src.getData()), stepSize_(src.getStepSize()){
}
[some more code ...]
protected:
itr data_;
difference_type stepSize_;
};
typedef iterator_<true> const_iterator;
typedef iterator_<false> iterator;
};
呢:
class container{
iterator begin(){
return iterator(getStuff());
}
const_iterator begin() const{
return const_iterator(getStuff());
}
private:
Stuff getStuff() const { [some code here] }
};
如果您有通用代码。
将其移动到执行该工作的私有方法。
好的,显然这仍然是最佳答案:
template <T>
class container{
template< typename I, typename C >
static I begin_impl( C & c ){
return I(data_.begin(), 1);
}
template< typename I, typename C >
friend I container<T>::begin_impl( C & c );
iterator begin(){
return container<T>::template begin_impl< iterator >( *this );
}
const_iterator begin() const{
return container<T>::template begin_impl< const_iterator >( *this );
}
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 使用 std::set 的 .begin() 和 .end() 函数会产生意想不到的结果
- 迭代器end()函数与指针算术无法使用
- 布尔函数"Control reaches end of non-void function"错误
- 当调用带有警告"control reaches end of non-void function"的函数时,(实际上)会发生什么?
- 从end()函数返回的迭代器中获取BST的最后一个节点
- 使用 C++11 的 begin() 和 end() 函数通过参数确定数组维度
- 避免通用容器中 begin() 和 end() 函数的重复代码
- begin() 和 end() 函数不应该是模板类 Vector 的成员函数吗?
- c++函数(错误:control may reach end of non - void function)
- 为我的向量包装器和迭代器返回'begin'和'end'函数返回什么?
- Begin()和end()作为基于范围的for循环中的成员函数