有 std::is_constructible
及其变体(用于复制、移动和默认构造函数)。
但是,有没有一种方法可以通过 constexpr 检查对象是否可以构造?
最佳答案
is there a way to do a constexpr check whether an object can be constructed at all?
您可以编写一个constexpr
函数,然后添加不同的检查(is_default_constructible
等),如下所示:
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
}
关于c++ - 有没有办法 constexpr 检查一个类是否有任何可访问的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73345836/