2

Within our team's code review I found out a strange usage of struct initialization under C++17 and I'm surprised that it works:

struct TestStruct {
  std::string test_str1;
  std::string test_str2;
  std::string test_str3;
};

TestStruct t{test_str1 : "col1", test_str3 : "col3"};
std::cout << "str1: " << t.test_str1 << "\nstr2:" << t.test_str2 << "\nstr3:" << t.test_str3;
 

and here's the result as follow:

str1: col1
str2: 
str3: col3

I couldn't find references for such usage but it looks like Constructors and member initializer lists with some ellision.

Can someone explain why it works? Thanks :)

starskyTW
  • 21
  • 2
  • 4
    [C struct initialization using labels. It works, but how?](https://stackoverflow.com/a/1601420) – 001 Dec 28 '22 at 06:18
  • Does not compile with my compiler. Appears to be a GCC extension to **C++**, borrowed from **C**. – Eljay Dec 28 '22 at 14:04

0 Answers0