使用可能引发c++的函数初始化const对象
Initializing a const object with a function that may throw c++
我正在编写一个函数,该函数调用一个可能抛出的函数来初始化const成员。问题是:如果我把初始化放在try块中,当它超出范围时就会被删除。如果我把初始化放在try块之外,我就无法捕获异常。解决方法是:
double functionA()
{
double a;
try
{
a=functionThatMayThrow();
}
catch(std::except&p)
{
std::throw_with_nested(std::runtime_error(__func__);
}
const double b=a;
//More code that uses b and computes x
return x;
}
我想知道是否有更好的方法。类似于:
double functionA()
{
const double b=functionThatMayThrow();
//catch the exception somehow and rethrow with nested information
//Unchanged
//More code that uses b and computes x
return x;
}
也许我只是对这个问题采取了一种完全错误的方法。你能给我一些建议吗?任何boost或c++11相关的解决方案也是不错的。
要向任何传递的异常添加一些跟踪信息,1可以这样做:
auto func_A()
-> double
{
try
{
// The complete original function body.
double const a = 2*some_func();
return a/2;
}
catch( ... )
{
throw_with_nested( runtime_error( __func__ ) );
}
}
但是,在任何地方重复这一点都可能有点乏味且容易出错,因此您可能希望集中逻辑,而将函数表示为例如
auto func_A()
-> double
{
return with_trace( __func__, [&]() -> double
{
// The complete original function body.
double const a = 2*some_func();
return a/2;
} );
}
现在,这需要一些支持机制,包括下行呼叫和上行呼叫,例如
#include <exception> // std::throw_with_nested
#include <iostream>
#include <stdexcept> // std::runtime_error, std::exception
#include <stdlib.h> // EXIT_FAILURE, EXIT_SUCCESS
#include <string> // std::string
#include <vector> // std::vector
namespace cppx {
using std::runtime_error;
using std::throw_with_nested;
template< class Func >
auto with_trace( char const* const func_name, Func const body )
-> decltype( body() )
{
try
{
return body();
}
catch( ... )
{
throw_with_nested( runtime_error( func_name ) );
}
}
namespace x_io
{
using std::endl;
using std::exception;
using std::cerr; // For client code.
using std::ostream;
using std::rethrow_if_nested;
namespace impl {
void print_on( ostream& stream, exception const& x, bool const is_nested )
{
stream << (is_nested? ">" : "!") << x.what() << endl;
try
{
rethrow_if_nested( x );
}
catch( exception const& nested_x )
{
print_on( stream, nested_x, true );
}
catch( ... )
{
stream << "<unknown exception type>" << endl;
}
}
} // namespace impl
auto operator<<( ostream& stream, exception const& x )
-> ostream&
{
impl::print_on( stream, x, false );
return stream;
}
} // namespace x_io
} // namespace cppx
namespace my {
using std::runtime_error;
using std::string;
using std::vector;
using cppx::with_trace;
bool fail_is_good;
auto some_func()
-> double
{
if( fail_is_good ) { throw runtime_error( "some_func" ); }
return 3.14;
}
auto func_A()
-> double
{
return with_trace( __func__, [&]() -> double
{
double const a = 2*some_func();
return a/2;
} );
}
void cpp_main( vector< string > const& args )
{
using std::cout; using std::endl;
my::fail_is_good = (args.size() > 1);
cout << func_A() << endl;
}
} // namespace my
auto main( int argc, char* argv[] )
-> int
{
using std::exception; using std::string; using std::vector;
try
{
my::cpp_main( vector<string>( argv, argv + argc ) );
return EXIT_SUCCESS;
}
catch( exception const& x )
{
using namespace cppx::x_io;
cerr << x << endl;
}
return EXIT_FAILURE;
}
1)不幸的是,Visual C++12.0(2013),即使在2013年11月的CTP版本中,也不支持std::throw_with_nested
。只需实现它就可以很容易地添加这种支持。此外,我记得它不支持__func__
,但该宏也很容易定义
相关文章:
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 我们是否需要为 C++ 中的多个函数初始化多个模板?
- 在C++中使用默认构造函数初始化对象的不同方法
- 使用默认构造函数初始化对象的不同方法
- 我们可以用参数化构造函数初始化结构的数组吗?
- 有没有办法使用该类的构造函数初始化另一个类的私有部分内的对象数组?
- 类内初始化与构造函数初始化列表的顺序
- 通过 C++ 中的重载构造函数初始化未知类型的变量
- 使用复制构造函数初始化 new[]
- 如何从子类的构造函数初始化父类的私有成员
- 在构造函数初始化列表中使用 std::variant
- 使用构造函数初始化结构还是在之后设置其值更好?
- C++ 没有匹配的构造函数初始化 []
- 我正在使用dev c ++,但收到错误(C++98'array'必须由构造函数初始化)
- 如何从静态类函数初始化静态类对象
- 使用所述填充构造函数初始化向量中的向量
- 如何同时创建一个具有两个或多个构造函数初始化的对象
- 使用 c++ 中的函数初始化数组
- 使用 lambda 函数初始化静态数据成员