此函数的问题

Issues with this function

本文关键字:问题 函数      更新时间:2023-10-16

我被告知这个函数有问题,但是在做研究并尝试自己使用它之后,我似乎找不到它有什么问题。有人只是想惹我吗?

std::string foo() throw()
{
    std::string s("hello world");
    return s;
}

根据您的编译器设置,如果为字符串内容分配后备内存失败,std::string可能会从其构造函数中抛出。这将违反您提出的throw()条款。

否则,代码很好,当然可以缩短:

std::string foo()
{
    return "hello world";
}