Skip to content

Instantly share code, notes, and snippets.

@Manu343726
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manu343726/5aaff75d94892bc62fd2 to your computer and use it in GitHub Desktop.
Save Manu343726/5aaff75d94892bc62fd2 to your computer and use it in GitHub Desktop.
piecewise_construct<T>(): Construct a T from tuple elements
#include "piecewise_construct.hpp"
struct foo
{
foo(int i, int j) :
i{i}, j{j}
{}
int i = 0, j = 0;
};
int main()
{
auto f = piecewise_construct<foo>(std::make_tuple(1,2));
}
#include <tuple>
#include <type_traits>
template<typename T, typename... Ts, std::size_t... Is>
T piecewise_construct_impl(const std::tuple<Ts...>& args, std::index_sequence<Is...>)
{
return { std::forward<std::decay_t<Ts>>(std::get<Is>(args))... };
}
template<typename T, typename... Ts>
T piecewise_construct(const std::tuple<Ts...>& args)
{
return piecewise_construct_impl<T>(args, std::index_sequence_for<Ts...>{});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment