If "designated initialization" is C++20 feature, why is the following code compiled with the given options?

72 Views Asked by At

AFAIK, "designated initialization" is a C++20 feature(ref: https://en.cppreference.com/w/cpp/language/aggregate_initialization).

However, the following code,

// main.cc
#include <iostream>

struct Person
{
  const char *name;
  int age;
};

int main(int argc, char *argv[])
{
  Person person = {
      .name = "Bob",
      .age = 24,
  };
  std::cout << person.name << "\n"
            << person.age << "\n";
  return 0;
}

compiles with the the command g++ -Wall -Werror -std=c++11 -o main main.cc (also std=c++17).

Why is this so?

My g++ --version:

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0

There are 0 best solutions below