Last active
November 26, 2015 19:45
-
-
Save tomilov/b81ac96dba9ac1a14691 to your computer and use it in GitHub Desktop.
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 <type_traits> | |
#include <iostream> | |
#include <cstdlib> | |
#define PP { std::cout << __PRETTY_FUNCTION__ << std::endl; } | |
struct descriptor | |
{ | |
std::size_t const s; | |
bool const * c; | |
bool const * d; | |
}; | |
template< descriptor const & x, bool c, bool d, typename ...types > | |
union U; | |
template< descriptor const & x, bool c, bool d > | |
union U< x, c, d > | |
{ | |
static_assert(c); | |
static_assert(d); | |
U() PP | |
~U() PP | |
}; | |
template< descriptor const & x, bool c, bool d, typename first, typename ...rest > | |
union U< x, c, d, first, rest... > // specialize against c and/or d values | |
{ | |
static constexpr std::size_t s = x.s - sizeof...(rest); | |
using tail = U< x, x.c[s], x.d[s], rest... >; | |
first head_; | |
tail tail_{}; | |
U() PP | |
~U() { PP; tail_.~tail(); } | |
}; | |
template< typename ...types > | |
struct V | |
{ | |
static constexpr bool c[sizeof...(types) + 1] = {std::is_trivially_constructible< types >{}..., true}; | |
static constexpr bool d[sizeof...(types) + 1] = {std::is_trivially_destructible< types >{}..., true}; | |
static constexpr descriptor x{sizeof...(types), c, d}; | |
U< x, x.c[0], x.d[0], types... > storage_; | |
V() PP | |
~V() PP | |
}; | |
template< typename ...types > | |
constexpr bool V< types... >::c[sizeof...(types) + 1]; | |
template< typename ...types > | |
constexpr bool V< types... >::d[sizeof...(types) + 1]; | |
template< typename ...types > | |
constexpr descriptor V< types... >::x; | |
int | |
main() | |
{ | |
struct S { S() { ; } ~S() { ; } }; | |
V< int, S, double > v; | |
(void)v; | |
S s; | |
(void)s; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment