有没有拿到警告行为不端的指定初始化的方法吗? [英] Is there a Way to Get Warned about Misbehaving Designated Initializers?

查看:119
本文介绍了有没有拿到警告行为不端的指定初始化的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99引入结构指定intializers的概念。因此,例如,给定的:

C99 introduced the concept of designated intializers for structs. So for example, given:

typedef struct {
    int c;
    char a;
    float b;
} X;

我可以初始化,如: X富= {.A ='\\ 1',.B = 2.0F,.C = 4}; 并调用:的printf(C =%d个\\ NA =%HHU \\ NB =%F,foo.c的,foo.a,foo.b); 将输出:

C = 4结果
  a = 1时搜索
  B = 2.000000

c = 4
a = 1
b = 2.000000

<一个href=\"http://stackoverflow.com/questions/18731707/why-does-c11-not-support-designated-initializer-list-as-c99/29337570?noredirect=1#comment46864214_29337570\">As这里提到这已经分配给 C 那么的令人惊讶的行为 A 然后 b ,我的独立初始化指定的顺序。

As mentioned here this has the "surprising behavior" of assigning to c then a then b, independent of the order of my designated initializers.

这成为一个真正的问题,如果我有这样的功能:

This becomes a real issue if I have functions like this:

int i = 0;

int f() {
    return ++i;
}

int g() {
    i += 2;
    return i;
}

int h() {
    i += 4;
    return i;
}

和我想初始化是这样的: X富= {.A =(char)的F().B = G().C = H()}; 现在,当我做的:的printf(C =%d个\\ NA =%HHU \\ NB =%F,foo.c的,foo.a,foo.b); 我得到:

And I want to initialize like this: X foo = {.a = (char)f(), .b = g(), .c = h()}; Now when I do: printf("c = %d\na = %hhu\nb = %f", foo.c, foo.a, foo.b); I get:

C = 4结果
  1 = 5结果
  B = 7.000000

c = 4
a = 5
b = 7.000000

问题是有没有我的初始化顺序没有得到尊重的警告。是否有警告或东西,我可以启用此?

The problem being there was no warning that my initialization order was not respected. Is there a warning or something I can enable for this?

[活生生的例子]

推荐答案

...没有任何警告,我的初始化顺序没有得到尊重的。

一个特定的初始化顺序的是基于其他的东西,然后在标准规定的期望。 (如在评论中指出)

A particular initialization order is an expectation based on something other then that stated in the standard. (as pointed out in the comments )

C99 的部分6.7.9,P23:23初始化列表前pressions的评价是indeterminately对于测序彼此,因此,
  其中出现的任何副作用顺序的未指定的。
  [重点煤矿]

C99 section 6.7.9, p23: 23 The evaluations of the initialization list expressions are indeterminately sequenced with respect to one another and thus the order in which any side effects occur is unspecified. [emphasis mine]

因此​​,有未定义除外(或未指定的)行为在这里没有问题。非常相似的其他C行为,如与函数参数的计算顺序的模糊性。

There is therefore no problem here except undefined (or unspecified) behavior. Very similar to other C behaviors such as the ambiguity with order of evaluation of function arguments.

修改结果
C99有这样一段话有关:

EDIT
C99 has this to say about that:

从C99§6.5.2.2p10:结果
  的函数参数的计算顺序是
  未指定的
的,功能指示器的评估的顺序,
  实际参数中的实际参数,SUBEX pressions是
  未指定的,但实际的呼叫前一个序列点。结果
   [重点煤矿]

from C99 §6.5.2.2p10:
Order of evaluation of function arguments is unspecified, The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
[emphasis mine]

在这里阅读更多

这是你preFER警告(你说好,+ 1)则是另一回事。我不知道这将是多么实用的是,虽然提供了警告 - - - undefined- - 行为 - 在C / C ++语言。

That you would prefer a warning (which you stated well, +1) is another matter. I am not sure how practical it would be though to provide a warning for -every- -undefined- -behavior- in the C/C++ languages.

有趣的是需要注意的一些既定的假设/意见 <一个href=\"http://stackoverflow.com/questions/18731707/why-does-c11-not-support-designated-initializer-list-as-c99\">in这个讨论 的为什么C ++标准不包括指定初始化。 (然而)...

It is interesting to note some of the stated assumptions/opinions in this discussion why the C++ standards do not include Designated Initializers. (Yet) ...

... C ++更感兴趣的是把灵活性的一边
  一种类型的设计师代替,因此设计人员可以很容易地使用
  正确且难以键入错误使用。

...C++ is more interested in putting the flexibility on the side of the designer of a type instead, so designers can make it easy to use a type correctly and difficult to use incorrectly.

这篇关于有没有拿到警告行为不端的指定初始化的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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