Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why boost::shared_ptr can not be compiled if BOOST_NO_EXCEPTIONS is defined in user.hpp

Tags:

c++

boost

I have an embeded system and would like to use boost in this system, but need to disable exception because I do not want to pay cost of exception.

Boost has given one user.hpp and settable macro option BOOST_NO_EXCEPTIONS and BOOST_NO_EXCEPTION_STD_NAMESPACE, but boost::shared_ptr can not be compiled(more precisely speaking, can not be linked) if these two macro is defined.

shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a): undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status

why does boost give macro options but do not promise compilation with those options?

like image 245
Liu Nick Avatar asked Feb 08 '23 14:02

Liu Nick


1 Answers

It can be compiled.

It just cannot be linked.

That is because if you define BOOST_NO_EXCEPTIONS, you'll have to provide the implementation of boost::throw_exception(std::exception const&) somewhere, to replace the usual error raising facilities.

Have a read through the comments in throw_exception.hpp:

namespace boost
{

#ifdef BOOST_NO_EXCEPTIONS

void throw_exception(std::exception const & e); // user defined

#else

//[Not user defined --Dynguss]
template<class E> inline void throw_exception(E const & e)  
{
    throw e;
}

#endif

} // namespace boost
like image 101
sehe Avatar answered Feb 16 '23 03:02

sehe