简体   繁体   English

C ++中的奇怪语法:return {.name = value,...}

[英]Odd syntax in C++: return { .name=value, … }

While reading an article, I came across the following function: 在阅读文章时,我遇到了以下功能:

SolidColor::SolidColor(unsigned width, Pixel color)
  : _width(width),
    _color(color) {}

__attribute__((section(".ramcode")))
Rasterizer::RasterInfo SolidColor::rasterize(unsigned, Pixel *target) {
  *target = _color;
  return {
    .offset = 0,
    .length = 1,
    .stretch_cycles = (_width - 1) * 4,
    .repeat_lines = 1000,
  };
}

What is the author doing with the return statement? 作者用return语句做了什么? I haven't seen anything like that before, and I do not know how to search for it... Is it valid for plain C too? 我之前没有见过这样的东西,我不知道如何搜索它...它对普通C也有效吗?

Edit: link to the original article 编辑: 链接到原始文章

This isn't valid C++. 这不是有效的C ++。

It's (sort of) using a couple features from C known as "compound literals" and "designated initializers", which a few C++ compilers support as an extension. 它是(某种程度上)使用C中的一些特性,称为“复合文字”和“指定初始化器”,一些C ++编译器支持它作为扩展。 The "sort of" comes from that fact that to be a legitimate C compound literal, it should have syntax that looks like a cast, so you'd have something like: “有点”来自于这样一个事实:要成为一个合法的C复合文字,它应该具有看起来像一个演员的语法,所以你有类似的东西:

return (RasterInfo) {
    .offset = 0,
    .length = 1,
    .stretch_cycles = (_width - 1) * 4,
    .repeat_lines = 1000,
  };

Regardless of the difference in syntax, however, it's basically creating a temporary struct with members initialized as specified in the block, so this is roughly equivalent to: 然而,无论语法有何不同,它基本上都是创建一个临时结构,其成员按块中的指定进行初始化,因此大致相当于:

// A possible definition of RasterInfo 
// (but the real one might have more members or different order).
struct RasterInfo {
    int offset;
    int length;
    int stretch_cycles;
    int repeat_lines;
};

RasterInfo rasterize(unsigned, Pixel *target) { 
    *target = color;
    RasterInfo r { 0, 1, (_width-1)*4, 1000};
    return r;
}

The big difference (as you can see) is that designated initializers allow you to use member names to specify what initializer goes to what member, rather than depending solely on the order/position. 最大的区别(如您所见)是指定的初始化程序允许您使用成员名称来指定初始化程序转到哪个成员,而不是仅仅依赖于顺序/位置。

It is a C99 compound literal . 这是一个C99 复合文字 This feature is specific to C99, but gcc and clang choose to implement it in C++ as well(as extension ). 此功能特定于C99,但gcc和clang也选择在C ++中实现它(作为扩展名 )。

6.26 Compound Literals 6.26复合文字

ISO C99 supports compound literals. ISO C99支持复合文字。 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++. 作为扩展,GCC支持C90模式和C ++中的复合文字,尽管语义在C ++中有些不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM