C ++ 11直接列表初始化语法 [英] C++11 direct-list-initialization syntax

查看:506
本文介绍了C ++ 11直接列表初始化语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了列表初始化及其各种口味之后,我决定在openGL ES应用程序中测试一些功能,我使用Xcode和Objective-C ++编写,直到我碰到一些比较模糊的东西。



我已经知道(并经常实现)常规的C风格结构初始化,使用以下语法初始化POD,例如GLKVector2,例如:

  GLKVector2 point = {0,0}; //复制初始化

但是这种方法可能并不总是程序员想要的。因此,通过删除赋值(和不必要的拷贝构造操作),有利于直接初始化,我们可以假设(从文档中)上面的声明会是这样:

  GLKVector2 point {0,0}; //直接初始化,但生成编译错误

但是,编译器不会在代码看起来像这样:

  GLKVector2点{{0,0}}; 

对我来说,这显示为 point 由从内部结构 {0,0} 创建的临时直接初始化,因此不提供优于第一种方法的任何优点;暂时仍然必须被分配和释放。



或者这个问题只是GLKit类型混淆编译器使用的union / struct布局的性质。


解决方案

有关这种奇怪语法的一些说明,在代码中进一步实现之前, div>

外括号定义对象本身的初始化器,内括号是对象内部成员的初始化器,例如

  GLKVector2 v = {initializers-for-members}; 

其中 initializers-for-members code> {0,0} ,因为类型有一个数组成员,有两个元素,你用 {a,b} / code>。



C ++支持brace elision(8.5.1 [dcl.init.aggr] paragraph 11),这意味着嵌套的大括号可以省略的初始化器在某些情况下,但在C ++ 11大括号删除只允许进行复制初始化。这是为什么你不需要两套大括号的copy-init情况,但确实需要它们直接初始化。



由于C ++ 11完成规则已由 DR 1270 更改为允许支架在直接列表初始化的情况下,但这是一个改变后的C ++ 11草稿,并没有得到广泛支持。


After reading up on list-initialization and its various flavours, I decided to test out some features in an openGL ES app I'm writing using Xcode and Objective-C++, until I ran into something rather obscure.

I've known about (and frequently implement) the conventional C style structure initialization using the following syntax to initialize POD, such as a GLKVector2, for example:

GLKVector2 point = { 0,0 };    //copy-initialized

but this approach may not always be what's intended by the programmer. So, by removing the assignment (and a needless copy construction operation), in favour of direct-initilization, one would assume (from the documentation) the above declaration would appear like so:

GLKVector2 point{ 0,0 };    //direct-initialized, but generates compile error

However, the compiler doesn't complain when the code looks like this:

GLKVector2 point{ { 0,0 } };

To me, this appears as point is being direct-initialized from a temporary created from the inner structure { 0,0 } and thus not offering any advantage over the first approach; the temporaries still have to be allocated and deallocated.

Or perhaps this issue is simply the nature of the union/struct layout used by GLKit types confusing the compiler.

Some clarification on this odd syntax, before further implementation in the code, would be much appreciated

解决方案

The outer braces delimit the initializer for the object itself, the inner braces are the initializer for a member inside the object, e.g.

GLKVector2 v = { initializers-for-members };

where initializers-for-members is { 0, 0 } because the type has an array member, of two elements, and you initialize an array of two members with { a, b }.

C++ supports "brace elision" (8.5.1 [dcl.init.aggr] paragraph 11) which means nested braces can be left out of the initializer under certain circumstances, but in C++11 brace elision is only allowed for copy-initialization. This is why you don't need two sets of braces for the copy-init case, but do need them for direct-init.

Since C++11 was completed the rules have been changed by DR 1270 to allow brace elision in the direct-list-initialization case too, but that's a change made to the post-C++11 draft and not widely supported yet.

这篇关于C ++ 11直接列表初始化语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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