c++ - 运算符重载以构造异常消息

标签 c++ exception operator-overloading c++03

我正在更改遗留代码库中的一些代码。这段代码中有一个经常重复的模式,如下所示:

std::stringstream ss;
ss << ...;
throw MyCustomException(ss.str());

因为无论如何我都在修改代码,所以我想制作如下内容:

throw MyCustomException() << ...;

有效地消除了对 std::stringstream 的需求.

我找到了一个解决方案:

struct MyCustomException: public std::runtime_error
{
    MyCustomException(const std::string& what_arg="")
    : std::runtime_error(what_arg)
    {
    }

#if 0
    template<typename T>
    friend
    MyCustomException operator<<(MyCustomException e, const T& obj)
    {
        std::stringstream ss;
        ss << e.what();
        ss << obj;
        return MyCustomException(ss.str());
    }
#else
    template<typename T>
    MyCustomException operator<<(const T& obj)
    {
        std::stringstream ss;
        ss << what();
        ss << obj;
        return MyCustomException(ss.str());
    }
#endif
};

两种解决方案 ( #if ... #endif) 都有效,但由于一切都是按值计算的,因此在抛出异常对象之前会创建大量拷贝。将签名更改为 MyCustomException& e产生大量编译时错误(为什么?)。

由于我绑定(bind)到仅支持 C++03 的旧 GCC 修订版,整个问题变得更加复杂。所以这里没有花哨的 C++1[147] 东西!

有没有更好的方法来实现我想要的功能 (throw MyCustomException() << ...;),而无需在抛出异常时创建许多临时拷贝?

最佳答案

[S]ince everything is by value, a lot of copies of the exception object are created before it is thrown

如果异常是异常的 ( as they should ),则运行时丢失不应该是您关心的事情。此外,copy elision可能会节省你的一天。简介并总结。

话虽如此,您可以通过使用 const-ref 删除一半的所谓拷贝:

struct MyCustomException: public std::runtime_error
{
    MyCustomException(const std::string& what_arg="")
    : std::runtime_error(what_arg)
    {}

    template<typename T>
    friend
    MyCustomException operator<<(MyCustomException const& e, T const& obj)
    {
        std::stringstream ss;
        ss << e.what();
        ss << obj;
        return MyCustomException(ss.str());
    }
};

关于c++ - 运算符重载以构造异常消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220524/

相关文章:

c++ - 分配取消引用的 shared_ptr

java 流 : elegant way to filter according an exception is thrown

c++ - C++ 中多重集的自定义 [] 访问

c++ - 运算符重载和多态区别

java - Java-7 中的自定义包装异常与 Multi-Catch

c++ - 在 C++17 中重载命名空间和子命名空间中的运算符是不明确的

c++ - 强制 String to int 函数消耗整个字符串

c++ - QtableWidget header 中的复选框

c++ - 使用索引几何 boost 多边形

Java 异常,捕捉什么,不捕捉什么?