这个功能的问题

Rya*_*man 1 c++

我被告知这个功能存在问题,但是在做了研究并试图自己使用之后,我似乎无法找到它的问题.有人只是想惹我?

std::string foo() throw()
{
    std::string s("hello world");
    return s;
}
Run Code Online (Sandbox Code Playgroud)

Cam*_*ron 7

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

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

std::string foo()
{
    return "hello world";
}
Run Code Online (Sandbox Code Playgroud)