#include #include #include using namespace std; template class Operation> struct BinExpr; template struct BasicType { using ObjType = RHS; }; template class Operation> struct BasicType> { using ObjType = typename BinExpr::ObjType; }; template class Operation> struct BinExpr { using ObjType = typename BasicType::ObjType; using value_type = typename ObjType::value_type; LHS const &d_lhs; RHS const &d_rhs; BinExpr(LHS const &lhs, RHS const &rhs) : d_lhs(lhs), d_rhs(rhs) {} size_t size() const { return d_lhs.size(); } value_type operator[](size_t ix) const { static Operation operation; return operation(d_lhs[ix], d_rhs[ix]); } operator ObjType() const { ObjType retVal; for (size_t ix = 0; ix != d_lhs.size(); ++ix) retVal.push_back((*this)[ix]); return retVal; } }; template BinExpr operator+(LHS const &lhs, RHS const &rhs) { return BinExpr(lhs, rhs); } int main() { vector one {1, 2, 3}; vector two {4, 5, 6}; vector three = one + two + (one + two); auto four = one + two + (one + two); auto z = "hello world"; cout << z << '\n'; cout << three[0] << ' ' << three[2] << '\n'; cout << four[0] << ' ' << four[2] << '\n'; }