8

Consider the following code:

constexpr auto f()
{
    auto str = "Hello World!";
    return str;
}

int main(int argc, char* argv[])
{
    static constexpr auto str = f();
    std::cout << str << std::endl;
    return 0;
}

Is that normal that my compiler does not display any warning? Is it defined behavior? Do I have the guarantee that the program will display "Hello World!"? I would expect the "Hello World!" not to live beyond the scope of the function...

2 Answers 2

14

In C++ string literals have static storage duration and live as long as the program runs. So, a pointer to a string literal returned from f is always valid. No allocation or deallocation is involved.

Note that string literals have type const char[N], which in your case decays to const char * due to auto type deduction. If your intent was to use std::string, you can directly construct it

auto str = std::string("Hello World!");

or use operator""s:

using std::string_literals;
auto str = "Hello World!"s;

However, since std::string is not a literal type, this values cannot be constexpr anymore.

Sign up to request clarification or add additional context in comments.

2 Comments

"string literals have type const char *" They don't.
@T.C. const char array of concrete length?
5

That's the second effect of auto. What you think it is, is not always what the compiler decides. This can leads to wrong programmer expectation - the rule is here compiler always win.

The fact here is that str is a const char * to a (static storage duration) string litteral. It can be fully determined at build time, so it is a valid constexpr.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.