BOOST _THROW_EXCEPTION导致无限递归

BOOST_THROW_EXCEPTION causes infinite recursion

本文关键字:无限 递归 EXCEPTION THROW BOOST      更新时间:2023-10-16

我使用的是boost 1.53.0,到目前为止没有任何问题(并且使用了套接字、计时器、容器、算法,所有这些都没有问题)。

我喜欢使用boost异常的想法,尤其是因为行号之类的原因。

然而,在我的(超级简单)代码中:

#include <iostream>
#include <fstream>
#include <boost/scoped_ptr.hpp>
#include <boost/exception/all.hpp>
struct my_error: virtual boost::exception, virtual std::exception { };
int main(int argc, char* argv[])
{
  try
  {
    BOOST_THROW_EXCEPTION(my_error());
  }
  catch(...)
  {
    std::cout <<"fail";
  }
}

用CMAKE生成的项目(希望不会搞砸)

cmake_minimum_required (VERSION 2.8)
project(error_test)
IF(WIN32)
   set(Boost_USE_STATIC_LIBS ON)
   set(Boost_USE_MULTITHREADED ON) 
   set(Boost_USE_STATIC_RUNTIME OFF)
   set(Boost_NO_SYSTEM_PATHS FALSE)
ENDIF()
find_package(Boost COMPONENTS system date_time)
include_directories(${Boost_INCLUDE_DIRS}
    )

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES})

BOOST_THROW_EXCEPTION不是抛出,而是进入无限递归!

编译器甚至捕捉到了这一点,声明编译器警告

警告C4717:"boost::exception_detail::throw_exception_":在所有控制路径上递归,函数将导致运行时堆栈溢出。

它一直在打击:

test.exe!boost::exception_detail::throw_exception_<my_error>(const my_error & x, const char * current_function, const char * file, int line)  Line 84 + 0xd1 bytes  C++

我正在使用visual studio 2010(获胜64)。我使用以下命令构建了boost,如果这有帮助的话:

.b2 install --prefix=C:devtoolsboost_1_53_0 --toolset=msvc --build-type=complete --build-dir=C:devtoolsbinboost_1_53_0 address-model=64 architecture=x86

EDIT添加扩展宏:

看起来宏扩展到

 ::boost::exception_detail::throw_exception_(my_error(), __FUNCSIG__  ,"main.cpp",40);

扩展到

 throw_exception_( E const & x, char const * current_function, char const * file, int line )
 {
     ::boost::exception_detail::throw_exception_(set_info( set_info( set_info( enable_error_info(x), throw_function(current_function)), throw_file(file)), throw_line(line)), __FUNCSIG__  ,"C:\devtools\boost_1_53_0\boost/throw_exception.hpp",91);

#第92行"C:\devtools\boost_1_53_0\boost/sthrow_exception.hpp"}

这太奇怪了。您可以轻松查看https://svn.boost.org/svn/boost/tags/release/Boost_1_53_0/boost/throw_exception.hppboost::exception::throw_exception_根本不是递归的。

我唯一能看到这种事情发生的方法就是使用邪恶的宏。请尝试在每个include指令前后将其放在主文件中。

#if defined(throw_exception) || defined(throw_exception_)
#error Somebody set us up the bomb
#endif

好的,所以出于某种原因,它看起来像

    throw_exception_( E const & x, char const * current_function, char const * file, int line )
    {
        boost::throw_exception(
            set_info(
                set_info(
                    set_info(
                        enable_error_info(x),
                        throw_function(current_function)),
                    throw_file(file)),
                throw_line(line)));
    }

已更改为

    throw_exception_( E const & x, char const * current_function, char const * file, int line )
    {
        BOOST_THROW_EXCEPTION(
            set_info(
                set_info(
                    set_info(
                        enable_error_info(x),
                        throw_function(current_function)),
                    throw_file(file)),
                throw_line(line)));
    }

在我的代码中。。。所以我一定打破了我自己的助推结构。对不起,这是一场徒劳的追逐!我投票决定关闭这个线程。。。