我开始编写一个类,其行为类似于std::vector
,但具有一些数学运算。主要是 vector 范数和重载重要数学运算符(+,-等)之类的东西,它们将按元素进行加,减。
该类发布在下面,我使用boost::operators
编写了所有数学运算符,并且它们都可以正常工作。在实现迭代器时,我遇到了一些问题。我试图将迭代器编写为嵌套类,并使用boost::iterator
获得std::vector
迭代器的大部分/全部功能。
这是我遇到麻烦的地方,代码将无法使用大约2英里的模板相关错误输出进行编译。如果您有兴趣,可以发给我,但它是典型的冗长的Boost模板错误。
我的问题有两个方面。
首先,构图是最佳的设计选择吗?使用私有(private)继承或装饰器模式可能会做得更好吗?或者,也许有人知道在图书馆中实现这一想法?
其次,迭代器在做什么?我感觉好像在boost::iterator
实现中缺少一些基本的东西,并且想要修复它而不是更改我的设计。
由于大多数方法微不足道或不重要,因此我并未将其包含在内。
//publicly inherits important boost::operators classes
template <class T>
class Coords: boost::arithmetic<Coords<T>
,boost::arithmetic<Coords<T>, T
// ,boost::indexable<Coords<T>,int,T&
// ,boost::dereferenceable<Coords<T>, T*>
// >
>
>
{
private:
//private member
std::vector<T> x_;
public:
//public constructors
Coords(int n, T x): x_(n,x){};
Coords(int n): x_(n){};
Coords(std::vector<T> &x);
Coords(const Coords &rhs);
//iterator nested class, public inherits boost::iterator_facade
class iterator: public boost::iterator_facade<iterator, Coords<T>, std::random_access_iterator_tag>{
private:
typename std::vector<T>::iterator iter_;
friend class boost::iterator_core_access;
void increment() { iter_ = iter_++;};
void decrement() { iter_ = iter_--;};
void advance(int n){ iter_ = iter_+=n;};
void equal(iterator const &other) const{
return this->iter_ == other.iter_;
}
T& dereference() const {return *iter_;};
int distance_to(iterator const &other) const{
return this->iter_ - other.iter_;
}
public:
iterator():iter_(0){};
explicit iterator(const typename Coords<T>::iterator& it):iter_(it.iter_){};
explicit iterator(const typename std::vector<T>::iterator& it):iter_(it){};
};
//methods from std::vector I would like to 'copy'
typename Coords<T>::iterator begin(){return iterator(x_.begin());};
typename Coords<T>::iterator end(){return iterator(x_.end());};
typename Coords<T>::iterator operator[](std::size_t n);
std::size_t size(){return x.size()};
//mathematical methods
T norm() const;
T square() const;
T sum() const;
T dotProd(const Coords &rhs);
//important operator overloads
Coords operator+=(const T &rhs);
Coords operator-=(const T &rhs);
Coords operator*=(const T &rhs);
Coords operator/=(const T &rhs);
Coords operator+=(const Coords<T> &rhs);
Coords operator-=(const Coords<T> &rhs);
Coords operator*=(const Coords<T> &rhs);
Coords operator/=(const Coords<T> &rhs);
};
最佳答案
这解决了该程序的许多问题:
#include <boost/operators.hpp>
#include <boost/iterator.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <vector>
//publicly inherits important boost::operators classes
template <class T>
class Coords: boost::arithmetic<Coords<T>
,boost::arithmetic<Coords<T>, T
// ,boost::indexable<Coords<T>,int,T&
// ,boost::dereferenceable<Coords<T>, T*>
// >
>
>
{
private:
//private member
std::vector<T> x_;
public:
//public constructors
Coords(int n, T x): x_(n,x){};
Coords(int n): x_(n){};
Coords(std::vector<T> &x);
Coords(const Coords &rhs);
//iterator nested class, public inherits boost::iterator_facade
class iterator: public boost::iterator_facade<iterator, T, boost::random_access_traversal_tag >{
private:
typename std::vector<T>::iterator iter_;
friend class boost::iterator_core_access;
void increment() { ++iter_;}
void decrement() { --iter_; }
void advance(int n){ iter_+=n;};
bool equal(iterator const &other) const{
return this->iter_ == other.iter_;
}
T& dereference() const {return *iter_;};
int distance_to(iterator const &other) const{
return this->iter_ - other.iter_;
}
public:
iterator():iter_(0){};
iterator(const iterator& it):iter_(it.iter_){};
iterator(iterator& it):iter_(it.iter_){};
explicit iterator(const typename std::vector<T>::iterator& it):iter_(it){};
};
//methods from std::vector I would like to 'copy'
iterator begin(){return iterator(x_.begin());};
iterator end(){return iterator(x_.end());};
iterator operator[](std::size_t n);
std::size_t size(){return x_.size();};
//mathematical methods
T norm() const;
T square() const;
T sum() const;
T dotProd(const Coords &rhs);
//important operator overloads
Coords operator+=(const T &rhs);
Coords operator-=(const T &rhs);
Coords operator*=(const T &rhs);
Coords operator/=(const T &rhs);
Coords operator+=(const Coords<T> &rhs);
Coords operator-=(const Coords<T> &rhs);
Coords operator*=(const Coords<T> &rhs);
Coords operator/=(const Coords<T> &rhs);
};
int main() {
Coords<int> c(3);
for(Coords<int>::iterator it(c.begin()); it != c.end(); ++it)
*it;
}
iterator_facade
的第三个模板参数是boost::
标签,而不是std::
标签。 iterator_facade
的第二个模板参数是值类型,而不是容器类型。 increment
,decrement
和advance
的代码都产生了(我认为)未定义的行为。 Coords<T>::
。