INT Q = {1,2};奇特的初始化列表

编程入门 行业动态 更新时间:2024-10-19 04:32:15
本文介绍了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 ,它提出了一个警告

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

现在的问题是:What用剩下的初始化happend?的这是一个错误。

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

不要混淆了,在 {1,2} 与逗号操作符的。作为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};奇特的初始化列表

本文发布于:2023-06-08 21:29:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇特   初始化   列表   INT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!