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

前端 未结 2 1391
情书的邮戳
情书的邮戳 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++.

    0 讨论(0)
  • 2021-01-07 18:45

    This isn't valid 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. 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:

    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.

    0 讨论(0)
提交回复
热议问题