7

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;
}
jms
  • 689
  • 1
  • 7
  • 18

2 Answers2

12

const and constexpr are not interchangeable, in your case you do not want to drop the const but you want to add constexpr like so:

constexpr const char* hello()
{
  return "hello world";
}

The warning you receive when you drop const, is because a string literal is an array of n const char and so a pointer to a string literal should be a *const char ** but in C a string literal is an array of char even though it is undefined behavior to attempt to modify them it was kept around for backwards compatibility but is depreciated so it should be avoided.

Community
  • 1
  • 1
Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
  • Makes sense, than you. What about a `constexpr` array, is there any difference under the hood between `constexpr const char* ptrToLiteral = "hello";` and `constexpr char cstring[] = "hello";`? If so, what? Is the latter stored in a different way? – jms Mar 21 '14 at 03:07
  • @user1062874 yes, the second case `cstring` is an modifiable array that will be initialized with a copy of the string literal `"hello"` while the first case `ptrToLiteral` will just be pointing to a string literal. – Shafik Yaghmour Mar 21 '14 at 03:10
  • I'm confused, note that in the original comment `cstring` is defined as `constexpr`, so it should not be modifiable, and attempting to modify `cstring` like `cstring[0] = 'a';` expectedly results in the error _assignment of read-only location 'cstring[0]'_ – jms Mar 21 '14 at 03:18
  • 1
    @user1062874 poor choice of words, `cstring` won't be modifiable in this case b/c it is a constexpr but would be otherwise. – Shafik Yaghmour Mar 21 '14 at 03:20
  • I thought that this would be better expressed as a separate question: http://stackoverflow.com/questions/22550084/difference-between-string-literal-and-constexpr-array-of-char – jms Mar 21 '14 at 04:22
2

Force constexpr evaluation:

constexpr const char * hi = hello();
std::cout << hi << std::endl;
Germán Diago
  • 6,892
  • 1
  • 30
  • 54