如何修改 C++ 的 What 字符串运行时错误?

发布于 2025-01-05 19:42:13 字数 458 浏览 4 评论 0原文

我有一个继承自 std::runtime_error 的类,如下所示:

#include <string>
#include <stdexcept>

class SomeEx : public std::runtime_error
{
public:
    SomeEx(const std::string& msg) : runtime_error(msg) { }
};

所说的 msg 将始终类似于“无效类型 ID 43”。有没有办法用另一个构造函数(或另一个方法)构建“什么字符串”,以便我只提供整数类型 ID?像这样的东西:

SomeEx(unsigned int id) {
    // set what string to ("invalid type ID " + id)
}

I have a class which inherits from std::runtime_error like so:

#include <string>
#include <stdexcept>

class SomeEx : public std::runtime_error
{
public:
    SomeEx(const std::string& msg) : runtime_error(msg) { }
};

Said msg will always be something like "invalid type ID 43". Is there any way to build that "what string" with another constructor (or another method) so that I provide only the integer type ID? Something like:

SomeEx(unsigned int id) {
    // set what string to ("invalid type ID " + id)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦旅人picnic 2025-01-12 19:42:13
static std::string get_message(unsigned int id) {
    std::stringstream ss;
    ss << "invalid type ID " << id;
    return ss.str();
}
SomeEx(unsigned int id) 
    : runtime_error(get_message(id)) 
{}

不相关:我们使用字符串 .what() 的原因是为了让人们停止使用错误数字。

static std::string get_message(unsigned int id) {
    std::stringstream ss;
    ss << "invalid type ID " << id;
    return ss.str();
}
SomeEx(unsigned int id) 
    : runtime_error(get_message(id)) 
{}

unrelated: the reason we have string .what() is so that people STOP USING ERROR NUMBERS.

没有伤那来痛 2025-01-12 19:42:13

当然: SomeEx(unsigned int id) : runtime_error(std::to_string(id)) { }

Sure: SomeEx(unsigned int id) : runtime_error(std::to_string(id)) { }

盛夏已如深秋| 2025-01-12 19:42:13

如果您可以将数字转换为字符串,那么您可以简单地附加它们:

#include <string>
#include <stdexcept>

std::string BuildMessage(std::string const&  msg, int x)
{
    std::string result(msg);

    // Build your string here
    return result;
}

class SomeEx : public std::runtime_error
{
    public:
        SomeEx(const std::string& msg)
            : runtime_error(BuildMessage(msg, id)) { }
};

If you can convert the number into a string then you can simply append them:

#include <string>
#include <stdexcept>

std::string BuildMessage(std::string const&  msg, int x)
{
    std::string result(msg);

    // Build your string here
    return result;
}

class SomeEx : public std::runtime_error
{
    public:
        SomeEx(const std::string& msg)
            : runtime_error(BuildMessage(msg, id)) { }
};
忱杏 2025-01-12 19:42:13

添加到 Mooing Duck 的答案中,现在您可以使用 lambda 并直接这样调用它:

SomeEx(unsigned int id) :
    std::runtime_error {
        [](const auto id) {
            std::ostringstream ss;

            ss << "invalid type ID " << id;
            return ss.str();
        }(id)
    }
{
}

Adding to Mooing Duck's answer that nowadays you can use a lambda and directly call it as such:

SomeEx(unsigned int id) :
    std::runtime_error {
        [](const auto id) {
            std::ostringstream ss;

            ss << "invalid type ID " << id;
            return ss.str();
        }(id)
    }
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文