简体   繁体   English

此功能的问题

[英]Issues with this function

I was told there is an issue with this function, however after doing research and trying to use it myself, I can't seem to find what is wrong with it. 有人告诉我此功能存在问题,但是在进行研究并尝试自己使用它之后,我似乎找不到任何问题。 Was someone just trying to mess with me? 有人只是想惹我吗?

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

Depending on your compiler settings, std::string may throw from its constructor if allocation of the backing memory for the string contents fails. 如果为字符串内容分配后备内存失败,则根据编译器设置, std::string可能会从其构造函数中抛出。 This would violate the throw() clause that you put. 这将违反您放置的throw()子句。

Otherwise, the code is fine, though of course it can be shortened: 否则,该代码就可以了,尽管可以将其缩短:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM