Skip to content

Instantly share code, notes, and snippets.

@vittorioromeo
Created June 26, 2017 15:08
Show Gist options
  • Save vittorioromeo/f4103b2076b3e352d91cb40858076bcb to your computer and use it in GitHub Desktop.
Save vittorioromeo/f4103b2076b3e352d91cb40858076bcb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <tuple>
namespace foo
{
template <typename T>
struct print final
{
const T& _x;
constexpr print(const T& x) noexcept : _x{x}
{
}
};
template <typename T>
auto& operator<<(std::ostream& os, const print<std::vector<T>>& v)
{
for(const auto& x : v._x) os << x << " ";
return os;
}
template <typename... Ts>
auto& operator<<(std::ostream& os, const print<std::tuple<Ts...>>& v)
{
(os << ... << print{std::get<Ts>(v._x)});
return os;
}
}
int main()
{
std::vector<int> v{1,2,3,4,5};
std::cout << foo::print{v} << std::endl;
auto t{std::make_tuple(std::vector<int>{1,2,3,4,5})};
std::cout << foo::print{v} << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment