【发布时间】:2017-03-14 12:52:21
【问题描述】:
如何检查某些类型是否可以从其他类型显式(反之亦然)构造?在这种情况下有什么 SFINAE 技巧吗?
我可以把is_explicitly_constructible写成combination of std::is_constructible and std::is_convertible:
#include <type_traits>
template <typename Type, typename Argument>
struct is_explicitly_constructible
: std::bool_constant
<
std::is_constructible<Type, Argument>::value &&
!std::is_convertible<Argument, Type>::value
>
{
};
但我是否考虑到此类代码中的所有可能情况?
【问题讨论】:
-
在我看来,将
declval与static_cast结合起来应该确定一种类型是否可以从另一种类型转换。将其与典型的 SFINAE 测试相结合,应该可以解决问题。 -
问题只是:这是正确的吗?如果是这样,是的。
-
@SamVarshavchik 但我想检查一种类型是否 constructible 来自另一种类型,而不是 convertible。而
static_cast支持转换非类本身的类型。 -
@Barry 这里没有陷阱吗?像竞争的构造函数或其他东西......
-
@Constructor
std::is_constructible<X,Y>做正确的事。它不检查构造函数X(Y )是否存在 - 只检查假设的X x(y);是否格式正确。
标签: c++ sfinae c++17 typetraits explicit