INT Q = {1,2};奇特的初始化列表 [英] int q = {1,2}; peculiar initialization list

查看:141
本文介绍了INT Q = {1,2};奇特的初始化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我横跨下面初始化来了,可以看出,VS2012
 显示错误抱怨太多的初始化。在GCC似乎
 返回的第一个元素作为值。

为什么这种奇特的初始化在GCC的支持?

 的#include<&stdio.h中GT;诠释的main()
{
    INT Q = {1,2};
    焦C = {'S','T','\\ 0'}; / * c是'S'* /
    的printf(%d个\\ N,Q); / *打印1 * /
}


解决方案

C11:6.7.9初始化(P11):


  

有关标量的初始化的的是单个的前pression,可选括在大括号


因此​​,这是允许

  INT Q = {1};

您可以括在大括号标量对象初始化( {} )。注意动词的的用在这里。标准说:

5.1.1.3诊断(P1):


  

如果一个preprocessing翻译单元或翻译单元包含违反任何语法规则或约束的符合标准的实现必须产生至少一种诊断消息(以实现定义的方式确定),即使该行为也明确指定为未定义或实现定义


所以,它是由它如何处理

编译器

  INT Q = {1,2};

编译于GCC 4.8.1与标志

-pedantic -Wall -Wextra ,它提出了一个警告

 【警告】在标量初始化多余的元素[默认启用]

现在的问题是:<一href=\"http://stackoverflow.com/questions/28412577/int-q-1-2-peculiar-initialization-list/28412748#comment45158611_28412577\">What用剩下的初始化happend?
这是一个错误


注意: C11:6.5.17(P3)说,的逗号操作符的不能出现在一个逗号用于分隔列表中的项目环境(如函数的参数或初始化的列表)。

不要混淆了 {1,2} 逗号操作符的。作为<一个href=\"http://stackoverflow.com/questions/28412577/int-q-1-2-peculiar-initialization-list/28412748?noredirect=1#comment45166370_28412748\">Keith汤普森指出,在前pression在初始化的是一个的分配-EX pression 的,它不能包含的逗号运营商的在顶层。这意味着它可以带括号前pression内或条件算子在这种背景下,第二前pression内使用。在函数调用

  F(A,(T = 3,T + 2),C)

该函数有三个参数,其中第二个具有值 5

I came across the below initialization , it is seen that VS2012 shows an error complaining about too many initializers. in GCC it seems to return the first element as the value.

why is this peculiar initialization supported in GCC?

#include <stdio.h>

int main()
{
    int q = {1,2};
    char c = {'s','t','\0'};  /* c is 's' */
    printf("%d\n",q); /* prints 1*/
}

解决方案

C11: 6.7.9 Initialization (p11):

The initializer for a scalar shall be a single expression, optionally enclosed in braces.

Therefore, this is allowed

int q = {1};   

You can enclose the initializer for scalar objects in braces ({}). Note the verb shall is used here. The standard says:

5.1.1.3 Diagnostics (P1):

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined

So, it is up to the compiler how it handles

int q = {1,2}; 

Compiled on GCC 4.8.1 with flags -pedantic -Wall -Wextra and it raised a warning

[Warning] excess elements in scalar initializer [enabled by default]   

Now the question is: What happend with the remaining initializers? It's a bug.


Note: C11: 6.5.17 (p3) says that the comma operator cannot appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists of initializers).

Do not confused the , in {1,2} with comma operator. As Keith Thompson pointed out that, the expression in initializer to be an assignment-expression and it must not contain comma operator at top-level. That means it can be used within a parenthesized expression or within the second expression of a conditional operator in such contexts. In the function call

f(a, (t=3, t+2), c)

the function has three arguments, the second of which has the value 5.

这篇关于INT Q = {1,2};奇特的初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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