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

前端 未结 2 1414
情书的邮戳
情书的邮戳 2021-01-07 18:22

While reading an article, I came across the following function:

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

         


        
2条回答
  •  悲哀的现实
    2021-01-07 18:38

    It is a C99 compound literal. This feature is specific to C99, but gcc and clang choose to implement it in C++ as well(as extension).

    6.26 Compound Literals

    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++.

提交回复
热议问题