gpt4 book ai didi

C++11 类型推导与 const char *

转载 作者:可可西里 更新时间:2023-11-01 15:51:12 24 4
gpt4 key购买 nike

GotW 94 , Herb Sutter 对“经典 C++”声明进行了区分

const char* s = "Hello";

和“现代”风格

auto s = "Hello";

他告诉我们“s 的类型有细微差别,auto 样式更正确”。 [编辑添加:评论表明这可能不公平地表达 Sutter 的实际意思;请参阅下面的讨论。]

但是……有什么区别?我的印象是 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 char *) s = 0x0000000100000f91 "Hello"
(const char *) t = 0x0000000100000f91 "Hello"

Sutter 所指的细微差别在哪里?

最佳答案

我将此发布到 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 *

这是他的回复:

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!

关于C++11 类型推导与 const char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27550539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com