gpt4 book ai didi

c++ - 初始化字符串时额外的大括号

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:27 25 4
gpt4 key购买 nike

根据问题What does string array[] = ""; mean and why does it work?我想问一下下面代码中s1s2有什么区别:

int main() {
const char* s1 = { "Hello" }; // strange but work as followed
const char* s2 = "Hello"; // ordinary case
return 0;
}

为什么允许额外的大括号?任何对 C++ 标准的引用都会很有用。

最佳答案

在 C++98(和 C++03)中这很简单;在第 8.5 条中:

14 - If T is a scalar type, then a declaration of the form T x = { a }; is equivalent to T x = a;

在 C++11 中,列表初始化 (8.5.4p3) 涵盖了这一点:

[...] if the initializer list has a single element of type E and either T is not a reference type or its referenced type is reference-related to E, the object or reference is initialized from that element [...]

认为这与Initializing scalars with braces 是同一个问题.

关于c++ - 初始化字符串时额外的大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21481925/

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