C ++ 11类型推导vs const char * [英] C++11 type deduction vs const char *

查看:187
本文介绍了C ++ 11类型推导vs const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GotW 94 < a>,Herb Sutter在经典C ++声明之间划分区别。

In GotW 94, Herb Sutter draws a distinction between the "classic C++" declaration

const char* s = "Hello";

和现代风格

auto s = "Hello";

他告诉我们, s的类型有一个 / code>,其中 auto 样式更正确。

He tells us that there's a "subtle difference in the type of s, where the auto style is more correct".

但是...有什么区别?我的印象是, const char * 是引用字符串文字的正确方法。此外,当我问我的调试器(lldb),它似乎认为类型实际上是一样的:

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"

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

Where's the subtle difference Sutter refers to?

推荐答案

你不完全清楚Herb说的是什么(上下文很重要),但无论如何类型是一样的。

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

这篇关于C ++ 11类型推导vs const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆