Questions tagged «static-cast»



4
使用boost :: shared_ptr进行static_cast?
static_cast与with等效boost::shared_ptr吗? 换句话说,我该如何重写以下内容 Base* b = new Derived(); Derived* d = static_cast<Derived*>(b); 什么时候使用shared_ptr? boost::shared_ptr<Base> b(new Derived()); boost::shared_ptr<Derived> d = ???

1
为什么`decltype(static_cast <T>(…))`并不总是`T`?
对于以下代码,除最后一个断言外,所有代码均通过: template&lt;typename T&gt; constexpr void assert_static_cast_identity() { using T_cast = decltype(static_cast&lt;T&gt;(std::declval&lt;T&gt;())); static_assert(std::is_same_v&lt;T_cast, T&gt;); } int main() { assert_static_cast_identity&lt;int&gt;(); assert_static_cast_identity&lt;int&amp;&gt;(); assert_static_cast_identity&lt;int&amp;&amp;&gt;(); // assert_static_cast_identity&lt;int(int)&gt;(); // illegal cast assert_static_cast_identity&lt;int (&amp;)(int)&gt;(); assert_static_cast_identity&lt;int (&amp;&amp;)(int)&gt;(); // static assert fails } 为什么最后一个断言失败,static_cast&lt;T&gt;而不总是返回a T?
24 c++  static-cast 

1
为什么在gcc的is_nothrow_constructible实现中需要static_cast?
取自GCC实施type_traits为什么static_cast在这里需要? template &lt;typename _Tp, typename... _Args&gt; struct __is_nt_constructible_impl : public integral_constant&lt;bool, noexcept(_Tp(declval&lt;_Args&gt;()...))&gt; {}; template &lt;typename _Tp, typename _Arg&gt; struct __is_nt_constructible_impl&lt;_Tp, _Arg&gt; : public integral_constant&lt;bool, // Why is `static_cast` needed here? noexcept(static_cast&lt;_Tp&gt;(declval&lt;_Arg&gt;()))&gt; {};
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.