Last active
August 29, 2015 14:26
-
-
Save Manu343726/5aaff75d94892bc62fd2 to your computer and use it in GitHub Desktop.
piecewise_construct<T>(): Construct a T from tuple elements
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 "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)); | |
} |
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 <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