Created
December 29, 2018 10:19
-
-
Save asford/5476a49305214231349e7181a8aec20e to your computer and use it in GitHub Desktop.
C++14 Compatible Tuple Operator Broadcast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tuple> | |
#include <utility> | |
namespace internal | |
{ | |
template<typename T, typename T2, size_t... Is> | |
void add_rhs_to_lhs(T& t1, const T2& t2, std::integer_sequence<size_t, Is...>) | |
{ | |
auto l = { (std::get<Is>(t1) += std::get<Is>(t2), 0)... }; | |
(void)l; | |
} | |
} | |
template <typename...T, typename...T2> | |
std::tuple<T...>& operator += (std::tuple<T...>& lhs, const std::tuple<T2...>& rhs) | |
{ | |
internal::add_rhs_to_lhs(lhs, rhs, std::index_sequence_for<T...>{}); | |
return lhs; | |
} | |
template <typename...T> | |
std::tuple<T...>& operator += (std::tuple<T...>& lhs, const std::tuple<T...>& rhs) | |
{ | |
internal::add_rhs_to_lhs(lhs, rhs, std::index_sequence_for<T...>{}); | |
return lhs; | |
} | |
template <typename...T, typename...T2> | |
std::tuple<T...> operator + (std::tuple<T...> lhs, const std::tuple<T2...>& rhs) | |
{ | |
return lhs += rhs; | |
} | |
template <typename...T> | |
std::tuple<T...> operator + (std::tuple<T...> lhs, const std::tuple<T...>& rhs) | |
{ | |
return lhs += rhs; | |
} | |
#include <iostream> | |
// see: http://en.cppreference.com/w/cpp/utility/integer_sequence | |
template<class Ch, class Tr, class Tuple, std::size_t... Is> | |
void print_tuple_impl(std::basic_ostream<Ch,Tr>& os, | |
const Tuple& t, | |
std::index_sequence<Is...>) | |
{ | |
((os << (Is == 0? "" : ", ") << std::get<Is>(t)), ...); | |
} | |
template<class Ch, class Tr, class... Args> | |
auto& operator<<(std::basic_ostream<Ch, Tr>& os, | |
const std::tuple<Args...>& t) | |
{ | |
os << "("; | |
print_tuple_impl(os, t, std::index_sequence_for<Args...>{}); | |
return os << ")"; | |
} | |
int main() | |
{ | |
std::tuple<int,int> a = {1,2}; | |
std::tuple<int,int> b = {2,4}; | |
std::cout << "a = " << a << '\n'; | |
std::cout << "b = " << b << '\n'; | |
std::tuple<int,int> c = a + b; // possible syntax 1 | |
std::cout << "c = " << c << '\n'; | |
a += b; // possible syntax 2 | |
std::cout << "a = " << a << '\n'; | |
a += std::make_tuple(3,4); // possible syntax 3 | |
std::cout << "a = " << a << '\n'; | |
a += {3, 4}; // possible syntax 3 | |
std::cout << "a = " << a << '\n'; | |
int x = 0; | |
int y = 0; | |
auto r = std::tie(x, y); | |
std::cout << "pre: " << x << "\n"; | |
r += a; | |
std::cout << "post: " << x << "\n"; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment