11

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?

user2862505
  • 241
  • 2
  • 8

2 Answers2

9

I posted this to Herb Sutter:

What do you think is the “subtle difference in the type of s, where the auto style is more correct”? See C++11 type deduction vs const char *

And here's his reply:

I think I had in mind that the auto version deduced an array, which it doesn’t. If I had something else in mind, I’ve forgotten it now, so I’ve removed that phrase and added your moniker to the Acknowledgments. Thanks for the feedback!

philipxy
  • 14,867
  • 6
  • 39
  • 83
8

You're not entirely clear on what Herb stated (context is important), but anyway the types are the same.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 4
    He didn't say any more (in that article) than the quote given in the question. I'm pretty sure you're right, and there's no difference, subtle or otherwise. – Mike Seymour Dec 18 '14 at 16:10
  • 1
    Thanks to this, and to the comment by @remyabel above. I'm sure you're correct that I didn't grasp exactly what his point was, but even after a several re-readings I'm not sure what he actually _is_ saying. That the auto version is more maintainable if there is a subtle change to the types, perhaps. – user2862505 Dec 18 '14 at 16:14