In GotW 94, Herb Sutter draws a distinction between the "classic C++" declaration
const char* s = "Hello";
and the "modern" style
auto s = "Hello";
He tells us that there's a "subtle difference in the type of s
, where the auto
style is more correct". [Edited to add: comments suggest that this might not be a fair representation of what Sutter actually meant; see discussion below.]
But... what's the difference? I was under the impression that a const char *
is the correct way to refer to a string literal. Further, when I asked my debugger (lldb), it seems to think the types are actually the same:
* thread #1: tid = 0x1756c2, 0x0000000100000f8f test`main + 31 at test.cc:4, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100000f8f test`main + 31 at test.cc:4
1 int main(void) {
2 const char* s = "Hello";
3 auto t = "Hello";
-> 4 return 0;
5 }
(lldb) fr v
(const char *) s = 0x0000000100000f91 "Hello"
(const char *) t = 0x0000000100000f91 "Hello"
Where's the subtle difference Sutter refers to?
You're not entirely clear on what Herb stated (context is important), but anyway the types are the same.
I posted this to Herb Sutter:
And here's his reply: