std::shared_ptr<T>reinterpret_pointer_cast(std::shared_ptr<U>&&r)noexcept; (8)(C++20 起) 创建std::shared_ptr的新实例,其存储指针从r的存储指针用转型表达式获得。 若r为空,则新的shared_ptr亦然(但其存储指针不必为空)。否则,新的shared_ptr将与r的初始值共享所有权,但若dynamic_pointer_cast所进...
cppreference-智能指针转型en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast dynamic_cast ...
避免使用dynamic_cast转而使用static_cast,并且在代码设计的时候保证static_cast的向下转换是安全且正确的...
static_cast转换 使用隐式和用户定义转换的组合来进行类型之间的转换。 语法 static_cast<目标类型 >(表达式 ) 返回目标类型 类型的值。 解释 只有下列转换在不移除常量性(或易变性)的场合才能用static_cast执行。 1)如果表达式 是 “cv1Base” 类型左值且目标类型 是“到cv2Deriv...
std::shared_ptr<T> static_pointer_cast( const std::shared_ptr<U>& r ) noexcept; (1) (C++11 起) template< class T, class U >std::shared_ptr<T> static_pointer_cast( std::shared_ptr<U>&& r ) noexcept; (2) (C++20 起) template< class T, class U >std::shared_ptr<T> ...
// std_tr1__memory__static_pointer_cast.cpp // compile with: /EHsc #include <memory> #include <iostream> struct base { int val; }; struct derived : public base { }; int main() { std::shared_ptr sp0(new derived); std::shared_ptr<derived> sp1 = std::static_pointer_cast<derived...
Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior. const_cast仅在其位于的那句语句中生效,下一条语句后,原const对象还是const属性。The cast lasts only for the remainder of the ...
使用SFINAE 测试指针类型是否可以 static_cast 到另一个指针类型 背景 我正在写一个可移动的QScopedPointer;基本上std::unique_pointer有一些额外的访问器。我在访问 C++11 兼容编译器之前就开始了它,但现在我决心把它做好(即使我在重新发明轮子)。 问题 让我们调用我的智能指针MyUniquePointer。 我需要知道 type ...
int* pi = reinterpret_cast<int*>(pf);简而言之,static_cast<> 将尝试转换,举例来说,如float-到-integer,而reinterpret_cast<>简单改变编译器的意图重新考虑那个对象作为另一类型。 指针类型(Pointer Types) 指针转换有点复杂,我们将在本文的剩余部分使用下面的类: ...
static_cast <new_type> (expression) 静态转换 静态转换是最接近于C风格转换,很多时候都需要程序员自身去判断转换是否安全。比如: double d=3.14159265; int i = static_cast<int>(d); 但static_cast已经有安全性的考虑了,比如对于不相关类指针之间的转换。参见下面的例子: ...