如何检查某个类型是否可以从其他类型显式构造(反之亦然)?在这种情况下,是否有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
>
{
};
但是我是否考虑到此类代码中的所有可能情况?
最佳答案
是的,这是正确的。可以从参数T
显式构造A
类型,如果
A
完全构造。也就是说,假设的T x(a)
是有效的。 T test() { return a; }
的格式不正确。 std::is_constructible
测试#1,而std::is_convertible
测试#2的有效性。因此,与#1和#2为is_explicitly_constructible
一样,想要#1而不是#2将是is_implicitly_constructible
。这样的
is_explicitly_constructible
/is_implicitly_constructible
对就是您将有条件地实现explicit
的构造函数的方法。例如,在libstdc++中,存在optional
的以下两个构造函数:implicit:
template <typename _Up = _Tp,
enable_if_t<__and_<
__not_<is_same<optional<_Tp>, decay_t<_Up>>>,
is_constructible<_Tp, _Up&&>, // (*)
is_convertible<_Up&&, _Tp> // (*)
>::value, bool> = true>
constexpr optional(_Up&& __t)
explicit:
template <typename _Up = _Tp,
enable_if_t<__and_<
__not_<is_same<optional<_Tp>, decay_t<_Up>>>,
is_constructible<_Tp, _Up&&>, // (*)
__not_<is_convertible<_Up&&, _Tp>> // (*)
>::value, bool> = false>
explicit constexpr optional(_Up&& __t);
您可以看到libstdc++使用与您相同的表达式。
关于c++ - 如何检查类型是否显式/隐式可构造?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42786565/