Here is a simple method:
template<typename T, typename A1, typename A2>
std::vector<T, A1> operator+( std::vector<T, A1> left, std::vector<T, A2> const& right ) {
left.insert( left.end(), right.begin(), right.end() );
return left;
}
int main() {
std::vector<string> m = { "a", "b" );
m = m + m + m;
}
but as @ChristianAmmer noted, overriding operator+ on a std::vector is ambiguous. And that would be wrong.
So you could go and write an entire infix named operator syntax, and use the magic of C++ to embed it in the C++ language, to get rid of that ambiguity. Sort of like this:
#include <utility>
template<typename Operation, short op>
struct InfixOp {
Operation* self() { return static_cast<Operation*>(this); }
Operation const* self() const { return static_cast<Operation const*>(this); }
};
template<typename first_type, typename Infix, short op>
struct PartialForm {
Infix const* infix;
first_type a;
template<typename T>
PartialForm(T&& first, Infix const* i):infix(i), a(std::forward<T>(first)) {}
};
#define OVERRIDE_OPERATORS(OP, CODE) \
template<\
typename Left,\
typename Operation\
>\
PartialForm<typename std::remove_reference<Left>::type, Operation, CODE> operator OP( Left&& left, InfixOp<Operation, CODE> const& op ) {\
return PartialForm<typename std::remove_reference<Left>::type, Operation, CODE>( std::forward<Left>(left), op.self() );\
}\
\
template<\
typename Right,\
typename First,\
typename Operation\
>\
auto operator OP( PartialForm<First, Operation, CODE>&& left, Right&& right )\
->decltype( (*left.infix)( std::move( left.a ), std::forward<Right>(right)) )\
{\
return (*left.infix)( std::move( left.a ), std::forward<Right>(right) );\
}
OVERRIDE_OPERATORS(+, '+')
OVERRIDE_OPERATORS(*, '*')
OVERRIDE_OPERATORS(%, '%')
OVERRIDE_OPERATORS(^, '^')
OVERRIDE_OPERATORS(/, '/')
OVERRIDE_OPERATORS(==, '=')
OVERRIDE_OPERATORS(<, '<')
OVERRIDE_OPERATORS(>, '>')
OVERRIDE_OPERATORS(&, '&')
OVERRIDE_OPERATORS(|, '|')
//OVERRIDE_OPERATORS(!=, '!=')
//OVERRIDE_OPERATORS(<=, '<=')
//OVERRIDE_OPERATORS(>=, '>=')
template<typename Functor, char... ops>
struct Infix:InfixOp<Infix<Functor, ops...>, ops>...
{
Functor f;
template<typename F>
explicit Infix(F&& f_in):f(std::forward<F>(f_in)) {}
Infix(Infix<Functor, ops...> const& o):f(o.f) {}
Infix(Infix<Functor, ops...>&& o):f(std::move(o.f)) {}
Infix(Infix<Functor, ops...>& o):f(o.f) {}
template<typename L, typename R>
auto operator()( L&& left, R&& right ) const
-> decltype( f(std::forward<L>(left), std::forward<R>(right)))
{
return f(std::forward<L>(left), std::forward<R>(right));
}
};
template<char... ops, typename Functor>
Infix<Functor, ops...> make_infix( Functor&& f )
{
return Infix<Functor, ops...>(std::forward<Functor>(f));
}
#include <vector>
struct append_vectors {
template<typename T>
std::vector<T> operator()(std::vector<T> left, std::vector<T>const& right) const {
left.insert(left.end(), right.begin(), right.end());
return std::move(left);
}
};
struct sum_elements {
template<typename T>
std::vector<T> operator()(std::vector<T> left, std::vector<T>const& right) const {
for(auto it = left.begin(), it2 = right.begin(); it != left.end() && it2 != right.end(); ++it, ++it2) {
*it = *it + *it2;
}
return left;
}
};
struct prod_elements {
template<typename T>
std::vector<T> operator()(std::vector<T> left, std::vector<T>const& right) const {
for(auto it = left.begin(), it2 = right.begin(); it != left.end() && it2 != right.end(); ++it, ++it2) {
*it = *it * *it2;
}
return left;
}
};
#include <iostream>
int main() {
auto append = make_infix<'+'>(append_vectors());
auto sum = make_infix<'+'>(sum_elements());
auto prod = make_infix<'*'>(prod_elements());
std::vector<int> a = {1,2,3};
a = a +append+ a +append+ a;
a = a +sum+ a;
a = a *prod* a;
std::cout << a.size() << "\n";
for (auto&& x:a) {
std::cout << x << ",";
}
std::cout << "\n";
}
which has the advantage of clarity at the point where you use it (I mean, a = a +append+ a is pretty clear what it intends to do), at the cost of being a bit tricky to understand how it works, and a bit verbose for such a simple problem.
But at least the ambiguity is gone, which is a good thing, right?