c++boosttype-traitsboost-variant

Is-braces-constructible type trait


How can I check whether specific type typename T is constructible from arguments typename ...Args in the manner T{Args...}? I aware of std::is_constructible< T, Args... > type trait from <type_traits>, but it works with parentheses, not curly braces. I do not have too much experience in writing of type traits, so I cannot provide initial example. As simplification we can accept any reasonable assertions, even if this leads to not too significant loss of generality.


Solution

  • template<class T, typename... Args>
    decltype(void(T{std::declval<Args>()...}), std::true_type())
    test(int);
    
    template<class T, typename... Args>
    std::false_type
    test(...);
    
    template<class T, typename... Args>
    struct is_braces_constructible : decltype(test<T, Args...>(0))
    {
    };