0
static struct inet_protosw inetsw_array[] =
{
    [0] = { /* assignment by index */
      .type =       SOCK_STREAM, /* assignment by field */
      .protocol =   IPPROTO_TCP,
      .prot =       &tcp_prot,
      .ops =        &inet_stream_ops,
      .no_check =   0,
      .flags =      INET_PROTOSW_PERMANENT |
              INET_PROTOSW_ICSK,
    },
}

The above code works in C, but not in C++. I think the 2 methods are really nice. How C++ remove it?

PS: It seems field assignment can be accomplished by: type: SOCK_STREAM

pengguang001
  • 4,045
  • 6
  • 27
  • 31
  • More on this, http://stackoverflow.com/questions/4900739/why-are-designated-initializers-not-implemented-in-g – jweyrich Sep 08 '11 at 02:43

2 Answers2

4

C and C++ are different languages. This code sample uses object initialization features that exist in C and do not exist in C++.

It's not that "C++ removed it", it's that it was not part of C back when C++ branched from it.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
0

If I remember correctly the [0] and .type initialization designators have been introduced by ISO C99, and since the C++ standard came out in 1998, it didn't incorporate these features.

And also in C++11 it is not possible. The relevant section in the standard is 8.5.1 Aggregates, and all the examples in that section are without designators.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121