Function returning a copy of an integer literal
int number() { return 1; }
can be easily converted to a simple compilation expression using the constexpr .
constexpr int number() { return 1; }
However, I am embarrassed when it comes to string literals. The regular method returns a pointer to const char , which points to a string literal,
const char* hello() { return "hello world"; }
but I think that just changing โconstโ to constexpr not what I want (as a bonus, it also gives a compiler warning with an outdated conversion from string constant to โchar *โ using gcc 4.7.1)
constexpr char* hello() { return "hello world"; }
Is there a way to implement hello() so that the call is replaced with a constant expression in the example below?
int main() { std::cout << hello() << "\n"; return 0; }
source share