C++11 - 将非静态数据成员声明为“自动" [英] C++11 - declaring non-static data members as 'auto'

查看:47
本文介绍了C++11 - 将非静态数据成员声明为“自动"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果非静态数据成员在声明中初始化,C++11 是否允许将非静态数据成员声明为自动"?例如:

Does C++11 allow declaring non-static data members as 'auto' if they are initialized in the declaration? For example:

struct S
{
    auto x = 5;  // in place of 'int x = 5;', which is definitely allowed
};

GCC 4.7 拒绝上述代码,但它接受 int x = 5;.

GCC 4.7 rejects the above code, while it accepts int x = 5;.

假设这不是编译器错误而是标准确实不允许这样做,为什么不呢?它与声明局部变量 auto 一样有用.

Assuming this is not a compiler bug but rather the standard really doesn't allow it, why not? It would be just as useful as declaring local variables auto.

推荐答案

禁止非静态成员的规则在7.1.6.4第4条:

The rule for prohibiting non-static members is in 7.1.6.4 clause 4:

auto 类型说明符也可用于声明变量选择语句 (6.4) 或迭代语句的条件(6.5), 在 type-specifier-seq 中的 new-type-id 或 type-idnew-expression (5.3.4),在 for-range-declaration 中,并在声明一个静态数据成员,带有出现在类定义 (9.4.2) 的成员规范中的大括号或等号初始化器.

The auto type-specifier can also be used in declaring a variable in the condition of a selection statement (6.4) or an iteration statement (6.5), in the type-specifier-seq in the new-type-id or type-id of a new-expression (5.3.4), in a for-range-declaration, and in declaring a static data member with a brace-or-equal-initializer that appears within the member-specification of a class definition (9.4.2).

我在这里找到了它是静态的理由,这反映了James McNellis 如何在评论中解释它.

I found the rationale for it being static here which reflects how James McNellis explains it in the comment.

一个国家机构不喜欢允许自动类型说明符非静电.从给作者的电子邮件:

One national body dislikes allowing the auto type-specifier for non-statics. From an e-mail to the authors:

    template< class T >
    struct MyType : T {
      auto data = func();
      static const size_t erm = sizeof(data);
    };

为了确定 X 的布局,我们现在有两阶段名称查找和 ADL.请注意, func 可以是类型或函数;它可以在 T 中找到,MyType 的命名空间,关联的实例化时 T 的命名空间、全局命名空间、匿名命名空间,或任何受 using 指令约束的命名空间.小心我们可能会抛出一些concept_map查找运气.根据标题包含的顺序,我什至可能会得到不同的 ADL 结果,并打破单一定义规则 -不需要确诊.

In order to determine the layout of X, we now have 2-phase name lookup and ADL. Note that func could be either a type or a function; it may be found in T, the namespace of MyType, the associated namespace(s) of T when instantiated, the global namespace, an anonymous namespace, or any namespaces subject to a using directive. With care we could probably throw some concept_map lookup in for luck. Depending on the order of header inclusion I might even get different results for ADL, and break the One Definition Rule - which is not required to be diagnosed.

由于这个争议,作者不再提出 auto允许非静态数据成员.

Because of this controversy, the authors no longer propose that auto be allowed for non-static data members.

所以,基本上根据头部包含的顺序,data 的类型可能会有很大不同.当然,auto x = 5; 不需要依赖于两阶段名称查找或 ADL,但是,我假设他们将其设为一揽子"规则,否则,他们将必须为每个用例制定单独的规则,这会使事情变得非常复杂.

So, basically depending on the order of header inclusion, the type of data could be very different. Of course, auto x = 5; would not need to depend on 2-phase name lookup or ADL, however, I'm a assuming that they made it a "blanket" rule because otherwise, they would have to make individual rules for every use case which would make things very complicated.

在同一篇论文中,作者提议取消这个限制,然而,这个提议似乎被拒绝了,可能是由于上述理由,而且无论初始化器是什么,预期的行为都可以是相同的.

In the same paper, the author proposes eliminating this restriction, however, it seems this proposal has been rejected probably due to the above rationale and also so that expected behavior can be the same no matter what the initializer is.

这篇关于C++11 - 将非静态数据成员声明为“自动"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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