template string representation of vector of objects - c++

I want to create a template function that generates a string representation of an array of things that have a name() method. The things might be kept by value, or by reference (raw or smart)
template< typename T>
struct GetStringRepresentation;
template< typename T>
struct GetStringRepresentation< std::vector< std::unique_ptr< T > > >
{
inline void ()( const std::vector< std::unique_ptr< T > >& seq, std::string& out )
{
size_t size = seq.size();
for (int i=0; i< size; i++)
{
if (i > 0)
out += ", ";
out += seq[i]->name();
}
}
};
template< typename T>
struct GetStringRepresentation< std::vector< std::shared_ptr< T > > >
{
inline void ()( const std::vector< std::shared_ptr< T > >& seq, std::string& out )
{
size_t size = seq.size();
for (int i=0; i< size; i++)
{
if (i > 0)
out += ", ";
out += seq[i]->name();
}
}
};
template< typename T>
struct GetStringRepresentation< std::vector< T* > >
{
inline void ()( const std::vector< T* >& seq, std::string& out )
{
size_t size = seq.size();
for (int i=0; i< size; i++)
{
if (i > 0)
out += ", ";
out += seq[i]->name();
}
}
};
template< typename T>
struct GetStringRepresentation< std::vector< T > >
{
inline void ()( const std::vector< T >& seq, std::string& out )
{
size_t size = seq.size();
for (int i=0; i< size; i++)
{
if (i > 0)
out += ", ";
out += seq[i].name();
}
}
};
As you can clearly see, there is a bunch of duplication, specially between the reference specializations. I'm not very up to speed with the best way to do this, i would like to see a better way that removes some, or all of the code duplication

Here's a tip to get you started: Specialize the print method only:
template <typename T> void print(T const & x)
{
std::cout << printer<T>::print(x) << std::endl;
}
template <typename T> struct printer
{
static std::string print(T const & x) { return x.name(); }
};
template <typename U> struct printer<U*>
{
static std::string print(U * p) { return p->name(); }
};
This way you only have to write the loop once, and the printer takes care of the details. You can even abstract this further and make a sort of is_pointer_like trait (everything that supports ->), which in turn you specialize for all the smart pointers:
printer<T, is_pointer_like<T>::value>::print(x); // etc.
template <typename T, bool> struct printer { /* as before */ }
template <typename T> struct printer<T, true>
{
static std::string print(T const & p) { return p->name(); }
};
// Traits:
template <typename T> struct is_pointer_like : std::false_type { };
template <typename U> struct is_pointer_like<U*> : std::true_type { };
template <typename U> struct is_pointer_like<std::shared_ptr<U>> : std::true_type { };
For a similar idea, see the pretty printer.

I would write this:
struct GetStringRepresentation
{
template<typename T>
std::string operator()(std::vector<T> const & seq)
{
std::string out;
size_t size = seq.size();
for (size_t i=0; i< size; i++)
{
if (i > 0)
out += ", ";
out += get(seq[i]); //call get() to get the string
}
return out;
}
private:
template<typename T>
std::string const & get(T const & t) { return t.name; }
template<typename T>
std::string const & get(T const * t) { return t->name; }
template<typename T>
std::string const & get(std::unique_ptr<T> const & t) { return t->name; }
template<typename T>
std::string const & get(std::shared_ptr<T> const & t) { return t->name; }
};

Related

expression templates - bad_alloc

i am currently working on a c++ project and now i am stuck already for a while. It's about delayed evaluation with expression templates and (for me at least) a strange bad_alloc.
If you try the code below, you'll notice runtime error bad_alloc due to the very last addition b+c. So thats the point where the delayed evaluation is done. Furthermore the code below compiles and runs fine if you remove the references of the members of "Expression" (left,right). But i need references there, due to performance, etc. . However i also dont see, why i cant use references there.
I've already spent a lot of time with it. Please let me know if somebody can help me.
Best Regards.
#include <iostream>
#include <vector>
template<typename value_t, typename left_t, typename right_t, typename op_t>
class Expression
{
public:
typedef value_t value_type;
explicit Expression(const left_t &left,
const right_t &right,
const op_t &op) :
left(left),
right(right),
op(op)
{
}
value_t operator [](const size_t &i) const
{
return op(left[i],right[i]);
}
size_t size() const { return left.size();}
private:
const left_t &left;
const right_t &right;
//const left_t left;
//const right_t right;
const op_t &op;
};
template<class left_t,
class right_t,
class value_t = typename left_t::value_type,
class op_t = std::plus<value_t>>
const Expression<value_t, left_t, right_t, op_t> operator +(const left_t &left,
const right_t &right)
{
return Expression<value_t,left_t,right_t,op_t>(left, right, op_t());
}
template<typename value_t, typename data_t = std::vector<value_t>>
class Vector : public data_t
{
public:
typedef value_t value_type;
using data_t::size;
Vector(const std::initializer_list<value_t> &list) :
data_t(list)
{
}
Vector(const size_t &n) :
data_t(n)
{
}
Vector(const Vector &v) :
data_t(v)
{
}
template<typename left_t, typename right_t, typename op_t>
Vector(const Expression<value_t,left_t,right_t,op_t> &v) :
data_t(v.size())
{
operator =(v);
}
template<typename vec_t>
Vector(const vec_t &v) :
data_t(v.size())
{
operator =(v);
}
template<typename vec_t>
Vector &operator =(const vec_t &v)
{
for(size_t i = 0; i < data_t::size(); ++i)
data_t::operator [](i) = v[i];
return (*this);
}
friend std::ostream &operator <<(std::ostream &os, const Vector &v)
{
if(v.size())
os << v[0];
for(size_t i = 1; i < v.size(); ++i)
os << " " << v[i];
return os;
}
};
int main()
{
Vector<double> a{0,1,2};
auto b = a+a+a;
auto c = a;
std::cout << a+a+a+a << std::endl;
std::cout << b+c << std::endl; // gives bad_alloc
return 0;
}
"But i need references there, due to performance, etc."
Prove it.
In expression templates, all¹ the information should be compile-time.
You can see my example here for a simple expression template:
// we have lazy placeholder types:
template <int N> struct placeholder {};
placeholder<1> _1;
placeholder<2> _2;
placeholder<3> _3;
// note that every type here is stateless, and acts just like a more
// complicated placeholder.
// We can have expressions, like binary addition:
template <typename L, typename R> struct addition { };
template <typename L, typename R> struct multiplication { };
// here is the "factory" for our expression template:
template <typename L, typename R> addition<L,R> operator+(L const&, R const&) { return {}; }
template <typename L, typename R> multiplication<L,R> operator*(L const&, R const&) { return {}; }
///////////////////////////////////////////////
// To evaluate/interpret the expressions, we have to define "evaluation" for each type of placeholder:
template <typename Ctx, int N>
auto eval(Ctx& ctx, placeholder<N>) { return ctx.arg(N); }
template <typename Ctx, typename L, typename R>
auto eval(Ctx& ctx, addition<L, R>) { return eval(ctx, L{}) + eval(ctx, R{}); }
template <typename Ctx, typename L, typename R>
auto eval(Ctx& ctx, multiplication<L, R>) { return eval(ctx, L{}) * eval(ctx, R{}); }
///////////////////////////////////////////////
// A simple real-life context would contain the arguments:
#include <vector>
struct Context {
std::vector<double> _args;
// define the operation to get an argument from this context:
double arg(int i) const { return _args.at(i-1); }
};
#include <iostream>
int main() {
auto foo = _1 + _2 + _3;
Context ctx { { 3, 10, -4 } };
std::cout << "foo: " << eval(ctx, foo) << "\n";
std::cout << "_1 + _2 * _3: " << eval(ctx, _1 + _2 * _3) << "\n";
}
So what you need is a literal type that holds a reference to the associated value, and defer all other evaluation to evaluation time.
I might prefer to add the size() operation as a free function, so that you don't have to encumber all the expression types with it (Separation Of Concerns).
¹ nearly, nl. except when encoding literals
Proof Of Concept
Using the strategy outlined:
Live On Coliru
#include <iostream>
#include <tuple>
namespace ETL {
template <typename T>
struct Literal {
T value;
T get() const { return value; }
};
/*
*template <typename T>
* static inline std::ostream& operator<<(std::ostream& os, ETL::Literal<T> const& lit) {
* return os << __PRETTY_FUNCTION__ << "\n actual: lit.value = " << lit.value;
* }
*/
template <class L, class R, class Op>
struct BinaryExpr : std::tuple<L, R, Op> { // tuple optimizes for empty element types
BinaryExpr(L l, R r, Op op)
: std::tuple<L, R, Op> { l, r, op }
{}
L const& get_lhs() const { return std::get<0>(*this); }
R const& get_rhs() const { return std::get<1>(*this); }
Op const& get_op() const { return std::get<2>(*this); }
};
template <class L, class R, class Op> auto cured(BinaryExpr<L,R,Op> _) { return _; }
template <class T> auto cured(Literal<T> l) { return std::move(l); }
template <class T> Literal<T> cured(T&& v) { return {std::forward<T>(v)}; }
template <class Op, class L, class R>
BinaryExpr<L, R, Op> make_binexpr(L&& l, R&& r) { return { std::forward<L>(l), std::forward<R>(r), Op{} }; }
template <class L, class R> auto operator +(L&& l, R&& r)
{ return make_binexpr<std::plus<>>(cured(std::forward<L>(l)), cured(std::forward<R>(r))); }
template <class L, class R> auto operator -(L&& l, R&& r)
{ return make_binexpr<std::minus<>>(cured(std::forward<L>(l)), cured(std::forward<R>(r))); }
template <class L, class R> auto operator *(L&& l, R&& r)
{ return make_binexpr<std::multiplies<>>(cured(std::forward<L>(l)), cured(std::forward<R>(r))); }
template <class L, class R> auto operator /(L&& l, R&& r)
{ return make_binexpr<std::divides<>>(cured(std::forward<L>(l)), cured(std::forward<R>(r))); }
template <class L, class R> auto operator %(L&& l, R&& r)
{ return make_binexpr<std::modulus<>>(cured(std::forward<L>(l)), std::forward<R>(r)); }
template <typename T> auto val(T const& v)
{ return cured(v); }
namespace impl {
template <class T>
static constexpr auto is_indexable(T const&) -> decltype(std::declval<T const&>()[0], std::true_type{}) { return {}; }
static constexpr auto is_indexable(...) -> decltype(std::false_type{}) { return {}; }
struct {
template <class T> size_t operator()(T const& v) const { return (*this)(v, is_indexable(v)); }
template <class T> size_t operator()(T const& v, std::true_type) const { return v.size(); }
template <class T> size_t operator()(T const&, std::false_type) const { return 0; }
template <class T> size_t operator()(Literal<T> const& l) const { return (*this)(l.value); }
template <class L, class R, class Op>
size_t operator()(BinaryExpr<L,R,Op> const& be) const { return (*this)(be.get_lhs()); }
} size;
struct {
template <class T>
auto operator()(size_t i, T const& v) const { return (*this)(i, v, is_indexable(v)); }
template <class T>
auto operator()(size_t i, T const& v, std::true_type) const { return v[i]; }
template <class T>
auto operator()(size_t, T const& v, std::false_type) const { return v; }
template <class T> auto operator()(size_t i, Literal<T> const& l) const { return (*this)(i, l.value); }
template <class L, class R, class Op>
auto operator()(size_t i, BinaryExpr<L,R,Op> const& be) const {
return be.get_op()((*this)(i, be.get_lhs()), (*this)(i, be.get_rhs()));
}
} eval_at;
}
template <typename T> size_t size(T const& v) { return impl::size(v); }
template <typename T> auto eval_at(size_t i, T const& v) { return impl::eval_at(i, v); }
}
#include <vector>
template <class value_t>
struct Vector : std::vector<value_t> {
using data_t = std::vector<value_t>;
typedef value_t value_type;
using data_t::data_t;
template <typename Expr>
Vector(Expr const& expr) { *this = expr; }
template <typename Expr>
Vector& operator=(Expr const& expr) {
this->resize(size(expr));
for (size_t i = 0; i < this->size(); ++i)
this->at(i) = eval_at(i, expr);
return *this;
}
friend std::ostream &operator<<(std::ostream &os, const Vector &v) {
for (auto& el : v) os << " " << el;
return os;
}
};
int main() {
Vector<double> a { 1, 2, 3 };
using ETL::operator+;
using ETL::operator*;
//std::cout << typeid(a + a * 4 / 2).name() << "\n";
#define DD(x) std::cout << typeid(x).name() << " size: " << ETL::size(x) << " result:" << (x) << "\n"
DD(a * -100.0);
auto b = a + a + a;
auto c = a;
std::cout << size(b) << "\n";
std::cout << (a + a + a + a) << "\n";
std::cout << a * 4.0 << "\n";
std::cout << b + c << "\n";
std::cout << (a + a + a + a) - 4 * a << "\n";
}
Prints
ETL::BinaryExpr<ETL::Literal<Vector<double>&>, ETL::Literal<double>, std::multiplies<void> > size: 3 result: -100 -200 -300
3
4 8 12
4 8 12
4 8 12
0 0 0

Is it possible to get rid of template specialisation to stop recursion?

I'm writing my own container class that also provides iterators. These iterators can be dereferenced and reveal then a sub-range of the original container, for which again an iterator can be obtained.
Currently, I've a template iterator class (using boost::iterator_facade) that dereferences to an Collection ("range") if L!=0 or to a T& (stored elements) if L==0. Is it possible combine both in one class, such that less duplicate code is needed?
template<typename T, int L>
class CollectionIter : public boost::iterator_facade<
CollectionIter<T,L>, // type it selfe
Collection<T,L-1>, // value type
boost::random_access_traversal_tag,
Collection<T,L-1> > // deref. type
{
public:
CollectionIter(T* ptr, const std::vector<int>& collectionSize_)
: pointer(ptr), collectionSize(collectionSize_) { }
T* element() { return pointer; }
private:
friend class boost::iterator_core_access;
bool equal(const CollectionIter<T,L>& other) const { return pointer==other.pointer; }
auto dereference() const { return Collection<T,L-1>(pointer, collectionSize); }
void increment() { pointer = pointer + stepsize(); }
void decrement() { pointer = pointer - stepsize(); }
void advance(size_t i) { pointer = pointer + i*stepsize(); }
auto distance_to(const CollectionIter<T,L>& other) { return (other.pointer - pointer)/stepsize(); }
int stepsize() { return collectionSize.at(L); }
T* pointer;
const std::vector<int>& collectionSize;
};
/* Groundlevel Collection: deref returns T& */
template<typename T>
class CollectionIter<T,0> : public boost::iterator_facade<
CollectionIter<T,0>,
T,
boost::random_access_traversal_tag >
{
public:
CollectionIter(T* ptr, const std::vector<int>& collectionSize_)
: pointer(ptr), collectionSize(collectionSize_) { assert(stepsize()==1); }
T* element() { return pointer; }
private:
friend class boost::iterator_core_access;
bool equal(const CollectionIter<T,0>& other) const { return pointer==other.pointer; }
T& dereference() const { return *pointer; }
void increment() { pointer = pointer + stepsize(); }
void decrement() { pointer = pointer - stepsize(); }
void advance(size_t i) { pointer = pointer + i*stepsize(); }
auto distance_to(const CollectionIter<T,0>& other) { return (other.pointer - pointer)/stepsize(); }
int stepsize() { return collectionSize.at(0); }
T* pointer;
const std::vector<int>& collectionSize;
};
I see only three differences in the two version of CollectionIter:
(1) the boost::iterator_facade() inherited class receive different arguments. You can solve this with std::conditional as suggested by Johannes Schaub; something like
public std::conditional< (L > 0U),
boost::iterator_facade<
CollectionIter<T, L>,
Collection<T, L-1U>,
boost::random_access_traversal_tag,
Collection<T, L-1U> >,
boost::iterator_facade<
CollectionIter<T, 0U>,
T,
boost::random_access_traversal_tag > >
(2) the assert(stepsize()==1); in the constructor is present only in the ground (L == 0U) version. You can modify it as
assert( (L > 0U) || (stepsize() == 1) );
(3) the recursive dereference() method is really different in the ground version. I'm not an expert of SFINAE but, if I'm not wrong, you can insert both as follows
template <int M = L, typename = std::enable_if_t<(M > 0U)>>
auto dereference () const
{ return Collection<T, L-1U>(pointer, collectionSize); }
template <int M = L, typename = std::enable_if_t<(M == 0U)>>
T & dereference () const
{ return *pointer; }
So the full class become (sorry: I've changed L in a std::size_t)
template <typename T, std::size_t L>
class CollectionIter :
public std::conditional< (L > 0U),
boost::iterator_facade<
CollectionIter<T, L>,
Collection<T, L-1U>,
boost::random_access_traversal_tag,
Collection<T, L-1U> >,
boost::iterator_facade<
CollectionIter<T, 0U>,
T,
boost::random_access_traversal_tag > >
{
public:
CollectionIter (T * ptr, const std::vector<int> & collectionSize_)
: pointer(ptr), collectionSize(collectionSize_)
{ assert( (L > 0U) || (stepsize() == 1) ); }
T* element() { return pointer; }
private:
friend class boost::iterator_core_access;
bool equal (const CollectionIter<T, L> & other) const
{ return pointer==other.pointer; }
template <int M = L, typename = std::enable_if_t<(M > 0U)>>
auto dereference () const
{ return Collection<T, L-1U>(pointer, collectionSize); }
template <int M = L, typename = std::enable_if_t<(M == 0U)>>
T & dereference () const
{ return *pointer; }
void increment ()
{ pointer = pointer + stepsize(); }
void decrement()
{ pointer = pointer - stepsize(); }
void advance (size_t i)
{ pointer = pointer + i*stepsize(); }
auto distance_to (const CollectionIter<T, L> & other)
{ return (other.pointer - pointer)/stepsize(); }
int stepsize()
{ return collectionSize.at(L); }
T * pointer;
const std::vector<int> & collectionSize;
};

Operator= doesn't work with std::vector c++ [duplicate]

This question already has answers here:
std::vector::resize() vs. std::vector::reserve()
(5 answers)
Closed 7 years ago.
I have a class
template <class T>
class General_matrix : public Math_object<T>
{
public:
General_matrix(const size_t m, const size_t n);
~General_matrix();
void init();
void show();
T* operator()(const size_t, const size_t) const;
General_matrix<T> operator*(const General_matrix<T>&);
};
derived from
template <class T>
class Math_object
{
protected:
size_t size_m, size_n;
std::vector <T> field;
public:
Math_object(const size_t m, const size_t n);
virtual ~Math_object() = 0;
virtual void show() = 0;
virtual void init() = 0;
};
Operator * doesn't work properly. The building of auxiliary matrix within an operator is OK, but when it comes to returning it changes only the integer data fields in receiving matrix. It turned out that the problem is with operator= for std::vector. I don't want to override it. Has anyone else faced this problem? Why can't I assign vector to vector of the same size?
template <typename T>
General_matrix<T> General_matrix<T>::operator*(const General_matrix<T> &right_operand)
{
General_matrix<T> aux(this->size_m, right_operand.size_n);
T collector;
for (int i=0; i<this->size_m; i++)
{
for (int j=0; j<right_operand.size_n; j++)
{
collector = 0;
for (int k=0; k<this->size_n; k++)
{
collector += *((*this)(i,k)) * *(right_operand(k,j));
}
*(aux(i,j)) = collector;
}
}
return aux;
}
UPD: Here is a MCVE
There are 2 matrices with values {{0,1,2},{3,4,5}} and {{0,1},{2,3},{4,5}}. The result of their multiplication must be {{10,13},{28,40}} and it is {{0,0},{0,0}}.
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Math_object
{
protected:
size_t size_m, size_n;
std::vector <T> field;
public:
Math_object(const size_t m, const size_t n);
virtual ~Math_object() = 0;
virtual void show() = 0;
virtual void init() = 0;
};
template <class T>
class General_matrix : public Math_object<T>
{
public:
General_matrix(const size_t m, const size_t n);
~General_matrix();
void init();
void show();
T* operator()(const size_t, const size_t) const;
General_matrix<T> operator*(const General_matrix<T>&);
};
template <typename T>
Math_object<T>::Math_object(const size_t m, const size_t n/*=1*/)
{
cout << "Constructor MO"<<"\n";
size_m=m;
size_n=n;
field.reserve(m*n);
}
template <typename T>
Math_object<T>::~Math_object()
{
cout << "Destructor MO"<<"\n";
vector<T>().swap(this->field);
}
template <typename T>
void Math_object<T>::show() {};
template <typename T>
General_matrix<T>::General_matrix(const size_t m, const size_t n):Math_object<T>(m,n)
{
cout << "Constructor GM"<<"\n";
}
template <typename T>
General_matrix<T>::~General_matrix()
{
cout << "Destructor GM"<<"\n";
}
template <typename T>
void General_matrix<T>::init()
{
cout << "Input matrix"<<"\n";
for (int i=0; i<this->size_m*this->size_n; i++)
this->field[i] = i;
}
template <typename T>
T* General_matrix<T>::operator()(const size_t i, const size_t j) const
{
return const_cast<T*>(&(this->field[i*(this->size_n)+j]));
}
template <typename T>
void General_matrix<T>::show()
{
for (int i=0; i < this->size_m; i++)
{
for (int j=0; j < this->size_n; j++)
{
cout << *((*this)(i,j)) << " ";
}
cout << "\n";
}
}
template <typename T>
General_matrix<T> General_matrix<T>::operator*(const General_matrix<T> &right_operand)
{
General_matrix<T> aux(this->size_m, right_operand.size_n);
T collector;
for (int i=0; i<this->size_m; i++)
{
for (int j=0; j<right_operand.size_n; j++)
{
collector = 0;
for (int k=0; k<this->size_n; k++)
{
collector += *((*this)(i,k)) * *(right_operand(k,j));
}
*(aux(i,j)) = collector;
}
}
return aux;
}
template class Math_object<int>;
template class Math_object<float>;
template class General_matrix<int>;
template class General_matrix<float>;
int main()
{
General_matrix<int> k(2,3);
k.init();
General_matrix<int> p(3,2);
p.init();
General_matrix<int> t(2,2);
t=k*p;
t.show();
return 0;
}
One problem, as mentioned in the comments, is the use of reserve instead of resize in the constructor.
But you have also undefined behavior in operator(). Better implement a const and a non-const version.

BOOST_FOREACH without Boost?

Is there an implementation of a "for each" macro like BOOST_FOREACH that doesn't need Boost?
I've made one.
It should work with C++11 r-value references in GCC and MSVC, but I haven't tested it well so if there's any errors please let me know.
Edit: I added support for strings.
#include <iterator>
#include <utility>
#define foreach(v, r) \
if (fe_detail::any const &fe_r_ = \
fe_detail::make_range(r)) { } else \
if (fe_detail::any const &fe_b_ = \
fe_detail::begin(fe_r_, ((void)0, 1) ? NULL : fe_detail::twrap(r))) { } else \
if (fe_detail::equal( \
fe_b_, \
fe_detail::end(fe_r_, ((void)0, 1) ? NULL : fe_detail::twrap(r)), \
((void)0, 1) ? NULL : fe_detail::twrap(r))) { } else \
for (bool fe_c_ = false; \
!fe_c_ && !fe_detail::equal( \
fe_b_, \
fe_detail::end(fe_r_, ((void)0, 1) ? NULL : fe_detail::twrap(r)), \
((void)0, 1) ? NULL : fe_detail::twrap(r)); \
fe_detail::advance(fe_b_, 1, ((void)0, 1) ? NULL : fe_detail::twrap(r))) \
for (v = (fe_detail::move(*fe_detail::iter(fe_b_, ((void)0, 1) ? NULL : fe_detail::twrap(r)))); \
fe_c_ = !fe_c_; )
namespace fe_detail
{
// Container traits
template<class C, class It> struct CT
{
typedef It It;
static It begin(C &c) { return c.begin(); }
static It end(C &c) { return c.end(); }
};
// Range traits
template<class R> struct RT : public CT<R, typename R::iterator> { };
template<class R> struct RT<R const> : public CT<R const, typename R::const_iterator> { };
template<class R> struct RT<R &> : public RT<R> { };
template<class T, bool B = T::value> struct enable_if;
template<class T> struct enable_if<T, true> { typedef T type; };
template<class T> struct is_char { static bool const value = false; };
template<class T> struct is_char<T const> : public is_char<T> { };
template<class T> struct is_char<T volatile> : public is_char<T> { };
template<> struct is_char<char> { static bool const value = true; };
template<> struct is_char<wchar_t> { static bool const value = true; };
template<class Ch> struct RT<Ch *> : enable_if<is_char<Ch> >
{
typedef Ch *It;
static It begin(It a) { return &a[0]; }
static It end(It a) { return &a[std::char_traits<Ch>::length(a)]; }
};
template<class R, size_t N> struct RT<R[N]>
{
typedef R *It;
static It begin(It a) { return &a[0]; }
static It end(It a) { return &a[N - (((void)0, is_char<R>::value) ? 1 : 0)]; }
};
template<class It> struct RT<std::pair<It, It> >
{
typedef It It;
static It begin(std::pair<It, It> const a) { return a.first; }
static It end(std::pair<It, It> const a) { return a.second; }
};
struct any { operator bool() const { return false; } };
template<class T> struct type_wrap { type_wrap(bool = false) { } };
template<class T> class wrap : public any
{ wrap &operator =(wrap const &); public: mutable T v; wrap(T v) : any(), v(v) { } };
template<class T> class wrap<T const> : public any
{ wrap &operator =(wrap const &); public: T const v; wrap(T const v) : any(), v(v) { } };
template<class T, size_t N> class wrap<T[N]> : public any
{ wrap &operator =(wrap const &); public: T (&v)[N]; wrap(T (&v)[N]) : any(), v(v) { } };
template<class T> class wrap<T const &> : public wrap<T const>
{ wrap &operator =(wrap const &); public: wrap(T const &v) : wrap<T const>(v) { } };
template<class T, size_t N> wrap<T[N]> make_range(T (&r)[N]) { return r; }
template<class T, size_t N> type_wrap<T[N]> twrap(T (&r)[N]) { throw 0; }
template<class It> type_wrap<std::pair<It, It> > twrap(std::pair<It, It> const &p) { throw 0; }
#if defined(_MSC_VER) && _MSC_VER >= 1600 || defined(__RVALUE_REFERENCE) || defined(__GXX_EXPERIMENTAL_CXX0X__)
template<class R> struct RT<R &&> : public RT<R> { };
template<class T> class wrap<T &&> : public wrap<T> { public: wrap(T &&v) : wrap<T>(std::move(v)) { } };
template<class R> wrap<R &&> make_range(R &&r) { return wrap<R &&>(std::forward<R>(r)); }
template<class R> type_wrap<R> twrap(R &&) { throw 0; }
using std::move;
#else
template<class R> wrap<R> make_range(R &r) { return r; }
template<class R> wrap<R const &> make_range(R const &r) { return r; }
template<class R> type_wrap<R> twrap(R &) { throw 0; }
template<class R> type_wrap<R const &> twrap(R const &) { throw 0; }
template<class T> T &move(T &v) { return v; }
template<class T> T const &move(T const &v) { return v; }
#endif
template<class R> wrap<typename RT<R>::It> begin(any const &r, type_wrap<R>)
{ return RT<R>::begin(static_cast<wrap<R> const &>(r).v); }
template<class R> wrap<typename RT<R>::It> end(any const &r, type_wrap<R>)
{ return RT<R>::end (static_cast<wrap<R> const &>(r).v); }
template<class R> bool equal(any const &i, any const &j, type_wrap<R>)
{ return static_cast<wrap<typename RT<R>::It> const &>(i).v == static_cast<wrap<typename RT<R>::It> const &>(j).v; }
template<class R> void advance(any const &i, typename std::iterator_traits<typename RT<R>::It>::difference_type const d, type_wrap<R>)
{ return std::advance(static_cast<wrap<typename RT<R>::It> const &>(i).v, d); }
template<class R> typename RT<R>::It &iter(any const &i, type_wrap<R>)
{ return static_cast<wrap<typename RT<R>::It> const &>(i).v; }
}
You can use it like:
#include <vector>
#include <iostream>
std::vector<int> make_vect()
{
std::vector<int> v;
v.push_back(5);
v.push_back(8);
v.push_back(10);
return v;
}
int main()
{
foreach (int c, make_vect())
{
std::cout << c << std::endl;
}
}

boost::iterator_range from a templated iterator

I'm trying to write a C++ version of Python's itertools.tee for Boost::Range (as seen here). Here is my first attempt:
template<typename R>
class tee_iterator : std::iterator<std::forward_iterator_tag, typename boost::range_value<R>::type>
{
public:
typedef typename boost::range_value<R>::type T;
typedef std::list<T> tee_queue;
typedef std::vector<tee_queue> tee_queue_collection;
tee_iterator(const R& r, tee_queue* q, tee_queue_collection* qs) :
it_(r.begin()), queue_(q), queues_(qs) {}
tee_iterator(const R& r) : it_(r.end()), queue_(NULL), queues_(NULL) {}
T& operator*() const { return current_; }
tee_iterator& operator++()
{
if (queue_->empty()) {
++it_;
for (auto q : queues_) {
q->push_back(*it_);
}
}
current_ = queue_->front();
queue_->pop_front();
return *this;
}
bool operator==(tee_iterator const& o) const { return it_ == o.it_; }
bool operator!=(tee_iterator const& o) const { return !(*this == o); }
private:
typedef typename boost::range_iterator<const R>::type const_iterator;
const_iterator it_;
tee_queue* queue_;
tee_queue_collection* queues_;
T current_;
};
template<typename R>
using tee_range = boost::iterator_range<tee_iterator<R> >;
template<typename R>
std::list<tee_range<R> > tee(const R& r, int n)
{
typedef typename boost::range_value<R>::type T;
typedef std::list<T> tee_queue;
typedef std::vector<tee_queue> tee_queue_collection;
tee_queue_collection queues(n);
std::list<tee_range<R> > ranges;
for (int i = 0; i < n; ++i) {
tee_range<R> t = { tee_iterator<R>(r, &queues[i], &queues), tee_iterator<R>(r) };
ranges.push_back(t);
}
return ranges;
}
but as soon as I try to use it:
int main(int argc, char* argv[])
{
std::list<int> l;
for (int i = 0; i < 10; ++i) {
l.push_back(i);
}
auto t = tee(l, 3);
}
it blows in my face, complaining about a missing value_type in boost::detail::iterator_traits<tee_iterator<std::list<int, std::allocator<int> > > > (and others). What am I missing? Isn't tee_iterator being a child of std::iterator enough?
You forgot to add public:
class tee_iterator : public std::iterator...