Im trying to use shared_ptr
in an embedded project which is build with xc32 1.34 (a derivative of gcc 4.5.2). The project has RTTI disabled with -fno-rtti
.
#include <memory>
Just including the header gives me the following errors:
/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory: In member function 'virtual void* std::tr1::_Ref_count_del<_Ty, _Dx>::_Get_deleter(const std::type_info&) const':
In file included from APP/MODULES/LIGHT_MANAGER/LightManager.cpp:13:0:
/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory:1264:39: error: cannot use typeid with -fno-rtti
/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory: In member function 'virtual void* std::tr1::_Ref_count_del_alloc<_Ty, _Dx, _Alloc>::_Get_deleter(const std::type_info&) const':
/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory:1299:39: error: cannot use typeid with -fno-rtti
/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory: In function '_Dx* std::tr1::get_deleter(const std::tr1::shared_ptr<_Ty2>&)':
/Applications/microchip/xc32/v1.34/bin/bin/../../lib/gcc/pic32mx/4.5.2/../../../../pic32mx/include/Cpp/memory:1956:44: error: cannot use typeid with -fno-rtti
So what i want to know is: is it generally impossible to use shared_ptr
without RTTI, or am I doing something wrong?
Since this function is specific to a given type, you don't need RTTI to perform the operations required by std::any .
The typeid operator requires RunTime Type Identification (RTTI) to be generated, which must be explicitly specified at compile time through a compiler option.
The shared_ptr type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory.
The problem is the get_deleter
free function:
template<class D, class T> D* get_deleter(const shared_ptr<T>& p) noexcept;
Returns: If
p
owns a deleterd
of type cv-unqualifiedD
, returnsstd:addressof(d)
; otherwise returnsnullptr
. The returned pointer remains valid as long as there exists ashared_ptr
instance that ownsd
.
Obviously, the most straightforward implementation of this is to store the typeid
of the deleter in the control block. While there are other possible implementations they would (a) be more complicated, (b) lose binary compatibility with RTTI-enabled code, and (c) be against the "spirit" of -fno-rtti
.
Another problematic function is dynamic_pointer_cast
, which calls dynamic_cast
on the stored pointer.
However, the main functionality of shared_ptr
is implementable without the use of RTTI features, and indeed as Sergei Nikulov mentions above, the shared_ptr
shipped with gcc 4.8.5 works with -fno-rtti
, with the exception of the get_deleter
and dynamic_pointer_cast
functions; as long as you do not use those facilities there is no reason you should not be able to use shared_ptr
. This can be contrasted with e.g. any
, which is not implementable without the use of typeid
.
It is the responsibility of your vendor to provide a standard library that works in all configurations of their compiler, including non-standard ones if they are supporting their use. However, if your vendor is noncooperative you still have a few options:
get_deleter
code;If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With