I am writing in C ++. I stumbled upon such code, I would like to know what is happening here and whether it is possible to repeat this in C ++.

static struct pci_driver my_driver = { .name = "my_pci_driver", .id_table = my_driver_id_table, .probe = my_driver_probe, .remove = my_driver_remove }; 

And also - why do you need to specify the struct keyword before declaring an instance of a structure, but not in C ++?

    2 answers 2

    And also - why do you need to specify the struct keyword before declaring an instance of a structure, but not in C ++?

    C and C ++ are different languages ​​with different rules.

    I would like to know what is happening here

    This is an initialization syntax tagged (tagged initializers), introduced in the C language in the standard C99. What exactly happens in my opinion is pretty obvious: you explicitly indicate which member of the structure you want to initialize with each value, thereby making the initializer independent of the order in which the fields are declared in the structure.

    can this be repeated in C ++

    C ++ does not yet support initialization syntax with tags, and there is no direct equivalent at the core level of the language. If I'm not mistaken, support for this syntax in some modified / abridged form is in plans for C ++ 20 . Previously, the proposals to support this syntax in C ++ were stubbornly rejected by the committee due to the attendant difficulties arising in the context of C ++ initialization.

      The g ++ compiler allows you to write in c ++ quite well. But this is an extension of the language and is not included in the standard.