Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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? 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?

Edit: link to the original article

like image 558
Fotis Panagiotopoulos Avatar asked Jul 23 '15 17:07

Fotis Panagiotopoulos


People also ask

How do you find odd values?

How do you determine if a number is odd or even? If a number is evenly divisible by 2 then it's an even number, otherwise, it's an odd number. We can also say when we divide a number by 2 and there is some remainder left, which is not divisible again by 2, then it's an odd number.

What is return value in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

What is an odd value?

Odd numbers are whole numbers that cannot be divided exactly into pairs. Odd numbers, when divided by 2, leave a remainder of 1. 1, 3, 5, 7, 9, 11, 13, 15 … are sequential odd numbers.


2 Answers

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.

like image 113
Jerry Coffin Avatar answered Oct 02 '22 04:10

Jerry Coffin


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

like image 20
Nazar554 Avatar answered Oct 02 '22 03:10

Nazar554