&(int){1}在C ++中是什么意思? [英] What does &(int) { 1 } mean in C++?

查看:152
本文介绍了&(int){1}在C ++中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处看到了,但我不知道这意味着什么:

I saw this here and I don't know what it means:

&(int) { 1 }

我认为这很奇怪,因为它看起来像无效的语法。它会在中间插入一个随机数为1(无分号)的块范围(?),并获取地址。对我来说没有多大意义。你们能启发我吗?

I thought it was weird because it seems like invalid syntax. It's casting a block scope(?) with a random 1 in the middle (without a semi-colon) and taking the address. Doesn't make a lot of sense to me. Could you guys enlighten me?

使用C ++ 11进行了尝试,因此可以编译:

Tried it out w/ C++11, so it compiles:

auto a = &(int) { 1 };

但是我不知道该变量如何处理。

But I have no idea what to do with the variable.

推荐答案

据我所知,这是复合文字,它是C99功能,它是不是标准C ++ ,但两者gcc和clang支持它作为扩展:

As far as I can tell this is a compound literal, it is C99 feature, it is not standard C++ but both gcc and clang support it as an extension:


ISO C99支持复合文字。复合文字看起来像包含初始化程序的强制类型转换。它的值是强制类型转换中指定类型的对象,其中包含初始化程序中指定的元素;它是一个左值。作为扩展,GCC在C90模式和C ++中支持复合文字,尽管语义在C ++中有所不同。

ISO C99 supports compound literals. A compound literal looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer; it is an lvalue. As an extension, GCC supports compound literals in C90 mode and in C++, though the semantics are somewhat different in C++.

通常,指定的类型是结构。假设如下所示声明了struct foo和
结构:

Usually, the specified type is a structure. Assume that struct foo and structure are declared as shown:

 struct foo {int a; char b[2];} structure;

以下是使用复合
文字构造struct foo的示例:

Here is an example of constructing a struct foo with a compound literal:

 structure = ((struct foo) {x + y, 'a', 0});

这等效于编写以下内容:

This is equivalent to writing the following:

 {
   struct foo temp = {x + y, 'a', 0};
   struct


在这种情况下, a 将指向int。希望这最初是C代码,因为gcc文档说:

In this case the type of a would pointer to int. Hopefully this was originally C code since as the gcc document says:


在C中,复合文字用静态或自动存储来指定未命名的对象持续时间。在C ++中,复合文字表示一个临时对象,该对象仅在其完整表达式结束之前一直存在。

In C, a compound literal designates an unnamed object with static or automatic storage duration. In C++, a compound literal designates a temporary object, which only lives until the end of its full-expression.

在C ++中,address可能不是一个好主意,因为对象的生命周期在完整表达式的结尾就结束了。虽然,可能是C ++代码只是依赖未定义的行为。

and so taking the address in C++ is probably a bad idea since the lifetime of the object is over at the end of the full-expression. Although, it could have been C++ code which just relied on undefined behavior.

这是在gcc和gcc中使用正确标志确实有很大帮助的情况之一使用 -pedantic 发出的叮当声将产生警告和错误,例如gcc说:

This is one of those cases where using the correct flags really helps a lot, in both gcc and clang using -pedantic will produce a warning and an error, for example gcc says:

warning: ISO C++ forbids compound-literals [-Wpedantic]
 auto a = &(int) { 1 };
                     ^
error: taking address of temporary [-fpermissive]

的地址使用 -fpermissive 是gcc,它确实允许编译此代码。尽管旧版本似乎允许使用 -Wno-address-of-temporary ,但在现代版本中,我无法使用clang来构建带有任何标志的代码。我想知道gcc是否允许它作为扩展

if we use -fpermissive is gcc it indeed does allow this code to compile. I can not get clang to build this code with any flags in modern versions although old versions seem to allow it using -Wno-address-of-temporary. I wonder if gcc allows this as a remnant of an old extension.

请注意,问题 C中的隐式结构定义使用复合文字非常有趣(取决于您对有趣的定义的使用)。

Note, the question Cryptic struct definition in C has a pretty interesting(depending on your definition of interesting) use of compound literals.

假定分裂是正确的这个问题是原始来源,然后是示例中该代码中的用法:

Assuming Schism is correct and this question is the original source then the use in that code in the example:

if (setsockopt(server_connection.socket, SOL_SOCKET, SO_REUSEADDR, &(int) { 1 }, sizeof(int)) < 0) {

在C99和C ++中都是有效的用法。

is a valid use in both C99 and C++.

这篇关于&amp;(int){1}在C ++中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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