Ryan Newman
Ryan Newman

Reputation: 856

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;
}

Upvotes: 1

Views: 58

Answers (1)

Cameron
Cameron

Reputation: 98736

Depending on your compiler settings, std::string may throw from its constructor if allocation of the backing memory for the string contents fails. This would violate the throw() clause that you put.

Otherwise, the code is fine, though of course it can be shortened:

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

Upvotes: 7

Related Questions