在GotW 94中,Herb Sutter 对“经典 C++”声明进行了区分
const char* s = "Hello";
和“现代”风格
auto s = "Hello";
他告诉我们,“类型的细微差别,s
风格auto
更正确”。[编辑添加:评论表明这可能不能公平地代表萨特的实际意思;见下文讨论。]
但是……有什么区别?我的印象是 aconst char *
是引用字符串文字的正确方法。此外,当我问我的调试器(lldb)时,似乎认为类型实际上是相同的:
* 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"
萨特所指的细微差别在哪里?