在GotW 94中,Herb Sutter区分了"经典C++"声明
const char* s = "Hello";
Run Code Online (Sandbox Code Playgroud)
和"现代"风格
auto s = "Hello";
Run Code Online (Sandbox Code Playgroud)
他告诉我们,有一个"中的类型细微的差别s,这里的auto风格是比较正确的." [编辑补充:评论表明,这可能不是Sutter实际意义的公平表示; 见下面的讨论.]
但是......有什么区别?我的印象是a const 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 …Run Code Online (Sandbox Code Playgroud)