简体   繁体   English

C++11:自动如何处理 () 初始化程序?

[英]C++11: how does auto deal with () initializer?

I know for C++11 way of initializing a vector using auto , actually an std::initializer_list is initialized instead of a vector .我知道 C++11 使用auto初始化vector的方式,实际上是初始化std::initializer_list而不是vector However, given below piece of code:但是,给出下面的代码:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    auto x = {1, 2};
    cout << typeid(x).name() << endl;
    auto z = (1, 2);
    cout << z << ", type: " << typeid(z).name() << endl;
    return 0;
}

I don't understand:我不明白:

  1. Why the type of x returned is St16initializer_listIiE and the type of 'z' returned is 'i', using gcc-10 compiler.为什么返回的x的类型是St16initializer_listIiE而返回的 'z' 的类型是 'i',使用 gcc-10 编译器。 Shouldn't we just return std::initializer_list and 'int'?我们不应该只返回std::initializer_list和 'int' 吗?
  2. There is a warning on z : warning: left operand of comma operator has no effect [-Wunused-value] . z上有一个警告: warning: left operand of comma operator has no effect [-Wunused-value] Then the 2nd half of result is: 2, type: i .然后结果的第二半是: 2, type: i How does c++11 interpret () -initialized type? c++11 如何解释() - 初始化类型? Why is only the last element passed into z and thus z is still of type int ?为什么只有最后一个元素传递给z ,因此z仍然是int类型?

Why the type of x returned is St16initializer_listIiE and the type of 'z' returned is 'i', using gcc-10 compiler.为什么返回的 x 的类型是 St16initializer_listIiE 而返回的 'z' 的类型是 'i',使用 gcc-10 编译器。 Shouldn't we just return std::initializer_list and 'int'?我们不应该只返回 std::initializer_list 和 'int' 吗?

typeid() won't directly give what you would expect, it just returns the specific type identification as written down in its code. typeid()不会直接给出您所期望的,它只是返回其代码中写下的特定类型标识。 If you would like to decrypt the same, pass it via c++filt :如果您想解密相同的内容,请通过c++filt传递它:

c++filt -t St16initializer_listIiE

It will result in what you were expecting, ie an:它将产生您所期望的结果,即:

std::initializer_list<int>

There is a warning on z: warning: left operand of comma operator has no effect [-Wunused-value]. z 上有警告:警告:逗号运算符的左操作数无效 [-Wunused-value]。 Then the 2nd half of result is: 2, type: i.然后结果的第二半是:2,输入:i。 How does c++11 interpret ()-initialized type? c++11如何解释()初始化的类型? Why is only the last element passed into z and thus z is still of type int?为什么只有最后一个元素传递到 z 中,因此 z 仍然是 int 类型?

( ) is an initializer holding an expression, or a list of expressions. ( )是一个包含表达式或表达式列表的初始值设定项。 If you were to assign that to auto , it would select the very last item in the expression list as its type as it would be seperated by commas till the advent of the last expression, which ideally tells auto to assign its type to the last one.如果要将其分配给auto ,它将 select 表达式列表中的最后一项作为其类型,因为它将用逗号分隔,直到最后一个表达式出现,理想情况下告诉 auto 将其类型分配给最后一个.

For Example:例如:

auto x = (1, 1.5L); 

would result in a long.会导致很长。

auto x = (1, 1.5f);

would result in a float.会导致浮动。

auto x = (1, "string");

would result in a const char pointer.将产生一个 const char 指针。

It entirely neglects the first value within the ( ) initializer, which is an int .它完全忽略了( )初始化程序中的第一个值,即int

The only thing that makes an initializer list is {} .唯一构成初始化列表的是{} In

auto z = (1, 2);

what you have is the comma operator which only returns that last value.你所拥有的是逗号运算符,它只返回最后一个值。 So that means your code boils down to所以这意味着你的代码归结为

auto z = 2;

and since 2 is an int , z is an int .因为2int ,所以zint

  1. Because { 1, 2 } is an std::initializer_list<int> , but (1, 2) is an expression, that expands to comma-operator (it evaluates both arguments and returns the second one as a result, so (1, 2) is collapsed to (2) , which is collapsed into 2 . That's the reason, why auto z = (1, 2); evaluates into an integer initialization.因为{ 1, 2 }是一个std::initializer_list<int> ,但(1, 2)是一个表达式,它扩展为逗号运算符(它同时计算 arguments 并返回第二个结果,所以(1, 2)被折叠成(2) ,它被折叠成2 。这就是为什么auto z = (1, 2);评估为 integer 初始化的原因。

  2. Because the result of instruction 1 is simply ignored (remember (1, 2) calculates both expressions and throws away the result of the first one).因为指令1的结果被简单地忽略(记住(1, 2)计算两个表达式并丢弃第一个的结果)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM