Is there a way to constexpr check if a class has any accessible constructor at all?

Viewed 58

There is std::is_constructible and its variants (for copy, move and default constructor).

However, is there a way to do a constexpr check whether an object can be constructed at all?

1 Answers

is there a way to do a constexpr check whether an object can be constructed at all?

You can write a constexpr function and then add different checks(is_default_constructible etc) as shown below:

class C 
{
    public:
       C() = default;
};
class C2
{
    public:
     C2() {}
    private:
     C2(const C2&){}
};
template<typename T>
constexpr bool checkAccessibleCtor()
{
    //write checks here according to your need
    return std::is_default_constructible<T>::value && 
        std::is_copy_constructible<T>::value && 
        std::is_move_constructible<T>::value;
}
int main()
{
    std::cout << checkAccessibleCtor<C>()<<std::endl;   //print 1
    std::cout << checkAccessibleCtor<C2>()<<std::endl; //prints 0
}
Related