A function returning a copy of an integer literal
int number()
{ return 1; }
can be easily converted to a plain compile-time expression using the keyword constexpr
.
constexpr int number()
{ return 1; }
However, I'm getting confused when it comes to string literals. The usual method is returning a pointer to const char
that points to the string literal,
const char* hello()
{ return "hello world"; }
but I think that merely changing "const" to constexpr
is not what I want (as a bonus, it also produces the compiler warning deprecated conversion from string constant to 'char*' using gcc 4.7.1)
constexpr char* hello()
{ return "hello world"; }
Is there a way to implement hello()
in such a way that the call is substituted with a constant expression in the example below?
int main()
{
std::cout << hello() << "\n";
return 0;
}