I need to use a traits function that deduces if a type can be aggregate initialized from given arguments. Typically it is like std::is_constructible
, but uses aggregate initialization in decltype()
.
template<typename POD, typename Tuple, typename = void_t<>>
struct is_aggregate_initializable_from_tuple : std::false_type
{
};
template<typename POD, typename ... Types>
struct is_aggregate_initializable_from_tuple<POD, std::tuple<Types...>, void_t<decltype(POD{Types()...})>>
: std::true_type
{
};
template<typename T, typename ... From>
struct is_aggregate_initializable : is_aggregate_initializable_from_tuple<T, std::tuple<From...>>
{
};
This works with MSVC and C++11, but does not compile with MinGW64. However it does compile with MinGW64 with C++14.
#include <string>
struct abc
{
int a = 4;
float b = 8.15f;
char c = 'c',
d = 'd';
int e = 16;
double f = 23.42;
std::string g = "oceanic";
};
int main()
{
static_assert(detail::is_aggregate_initializable<abc>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int, float>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int, float, char>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int, float, char, char>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int, float, char, char, int>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int, float, char, char, int, double>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, int, float, char, char, int, double, std::string>(), "Unexpected");
static_assert(detail::is_aggregate_initializable<abc, abc>(), "Unexpected");
return 0;
}
Is this a bug? What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire