c++之为什么在 gcc 的 is_nothrow_constructible 实现中需要 static_cast
lovecherry
阅读:19
2024-11-24 20:56:43
评论:0
取自 type_traits
的 GCC 实现为什么是 static_cast
这里需要吗?
template <typename _Tp, typename... _Args>
struct __is_nt_constructible_impl
: public integral_constant<bool, noexcept(_Tp(declval<_Args>()...))> {};
template <typename _Tp, typename _Arg>
struct __is_nt_constructible_impl<_Tp, _Arg>
: public integral_constant<bool,
// Why is `static_cast` needed here?
noexcept(static_cast<_Tp>(declval<_Arg>()))> {};
请您参考如下方法:
如果发明的变量声明,则类型不能从参数列表构造
T t(declval<Args>()...);
将是 well-formed并且是 known not to throw exceptions .在复数参数情况下,这等价于(模 noexcept 可破坏性,参见 LWG 2116)到 type conversion expression 的格式良好和不抛出
T(declval<Args>()...)
然而,在单参数情况下,表达式
T(declval<Args>())
被视为
cast-expression ,它可以调用
const_cast
and reinterpret_cast
;显式使用
static_cast
恢复与申报表的等效性。
作为 concrete example ,考虑以下类型:
struct D;
struct B { operator D&&() const; };
struct D : B {};
这里有一个
static_cast
来自
B const
至
D&&
必须使用转换运算符,但强制转换表达式可以绕过转换运算符,因此 noexcept 也是如此。所以省略了
static_cast
is_nothrow_constructible<D&&, B const>
会给出错误的结果.
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。