BOOST _THROW_EXCEPTION导致无限递归
BOOST_THROW_EXCEPTION causes infinite recursion
我使用的是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)));
}
在我的代码中。。。所以我一定打破了我自己的助推结构。对不起,这是一场徒劳的追逐!我投票决定关闭这个线程。。。
相关文章:
- 交换运算符 + 重载会导致无限递归
- 为什么当函数参数未定义为常量引用时存在无限递归?
- 为什么这是无限递归
- 尝试"复制"shared_ptr向上转换行为会导致复制构造函数上的无限递归(导致段错误)
- ConstexPR模板功能的无限递归
- 交换和移动无限递归
- 无限循环与无限递归.两者都未定义吗?
- 函数中的无限递归
- 运算符重载流提取运算符 (>>) C++会导致无限递归流提取
- C :Ostream无限递归
- 无限递归模板实例化使用clang时GCC工作正常
- 当我使用 boost 构建绝对路径时,无限递归
- 模板中的无限递归
- C++中的无限递归快速排序
- 尽管有停止条件,无限递归调用仍会执行,因为参数不会前进
- 在映射中插入地址时,新运算符重载会导致无限递归
- 可变参数模板实例化中的无限递归,试图构建任意深度的树状结构
- 调用原始版本的DLL挂钩函数时,我得到了无限递归
- std::swap 在 VS 2013 中导致无限递归
- 卡在无限递归中