class Base {};class Derived : public Base {};Derived d;Base& b = d; // OK (это upcast)
std::vector<Derived*> ds;ds.push_back(new Derived);ds.push_back(new Derived);std::vector<Base*>& bs = ds; // Error
std::vector<Base*>& bs = reinterpret_cast<std::vector<Base*>&>(ds);
std::vector<Base*>& bs = container_items_static_cast<Base*>(ds); // ОК
C++ (Qt)std::vector<Base*>& bs = reinterpret_cast<std::vector<Base*>&>(ds);
C++ (Qt)template <class _DestContainer>struct container_cast{ template <class _SourceContainer> static _DestContainer strict(_SourceContainer source) { const bool can_cast = std::is_same<_SourceContainer, _DestContainer>::value; static_assert(can_cast, "Incomplete cast"); return static_cast<_DestContainer>(source); }};
C++ (Qt)template < template<typename, typename> class _DestContainerTemplate , typename _DestItem, typename _DestAlloc>struct container_cast<_DestContainerTemplate<_DestItem, _DestAlloc> &>{ typedef _DestContainerTemplate<_DestItem, _DestAlloc> DestContainer; template < template<typename, typename> class _SourceContainerTemplate , typename _SourceItem, typename _SourceAlloc> static DestContainer & strict(_SourceContainerTemplate<_SourceItem, _SourceAlloc> &source) { const bool is_compatible_containers = std::is_same< _SourceContainerTemplate<_SourceItem, _SourceAlloc> , _DestContainerTemplate<_SourceItem, _SourceAlloc>>::value && std::is_same< _SourceContainerTemplate<_DestItem, _DestAlloc> , _DestContainerTemplate<_DestItem, _DestAlloc>>::value; static_assert(is_compatible_containers, "Incompatible containers"); typedef typename std::remove_pointer<_SourceItem>::type SourceClass; typedef typename std::remove_pointer<_DestItem>::type DestClass; const bool is_compatible_inheritance = std::is_base_of<DestClass, SourceClass>::value; static_assert(is_compatible_inheritance, "Incompatible inheritance"); return reinterpret_cast<DestContainer &>(source); }};
C++ (Qt)template <class _DestItem>struct container_items_cast{ template < template<typename, typename> class _ContainerTemplate , typename _SourceItem, template<typename> class _Allocator> static _ContainerTemplate<_DestItem, _Allocator<_DestItem>> & strict(_ContainerTemplate<_SourceItem, _Allocator<_SourceItem>> &source) { typedef _ContainerTemplate<_DestItem, _Allocator<_DestItem>> DestContainer; return container_cast<DestContainer &>::strict(source); }};
C++ (Qt)class Base {};class Derived : public Base {};class Other {}; std::vector<Derived*> derived_vector;derived_vector.push_back(new Derived);derived_vector.push_back(new Derived); std::list<Derived*> derived_list;derived_list.push_back(new Derived);derived_list.push_back(new Derived); std::vector<Base*>& base_vector = container_cast<std::vector<Base*>&>::strict(derived_vector);std::list<Base*>& base_list = container_cast<std::list<Base*>&>::strict(derived_list); // Error: Incompatible containers//std::list<Base*>& base_list_vector = container_cast<std::list<Base*>&>::strict(derived_vector);// Error: Incompatible inheritance//std::vector<Other*>& other_vector = container_cast<std::vector<Other*>&>::strict(derived_vector); std::vector<Base*>& base_vector_by_items = container_items_cast<Base*>::strict(derived_vector);std::list<Base*>& base_list_by_items = container_items_cast<Base*>::strict(derived_list);
C++ (Qt)std::vector<Base*>& bs = static_cast<std::vector<Base*>&>(ds);
const bool is_compatible_containers = std::is_same< _SourceContainerTemplate<_SourceItem, _SourceAlloc> , _DestContainerTemplate<_SourceItem, _SourceAlloc>>::value
C++ (Qt)const int dummy_object = 444555;_SourceItem const source_item = (_SourceItem) dummy_object;_DestItem const dest_item = static_cast<_DestItem> (source_item);const bool is_compatible_inheritance = (int) dest_item == dummy_object;static_assert(is_compatible_inheritance, "Incompatible inheritance");
C++ (Qt)Base ** b = &vec1[0];Derived ** d = &vec2[0];