-3

I was curious if the modern c++ standards people have considered or added the "Object Initializer" syntax of C#. For example, in C# I can initialize the members of an object like this during initialization:

StudentName student2 = new StudentName
{
    FirstName = "Craig",
    LastName  = "Playstead"
};

public class StudentName {
    public string FirstName;
    public string LastName;
};

Would be really handy if the standards people had planned to add "object initializer" syntax this to modern C++, ie. C++11, C++14, C++17, C++20, etc...

Does it currently exist in the modern C++ specification?

Bimo
  • 5,987
  • 2
  • 39
  • 61
  • [this answer](https://stackoverflow.com/a/45772324/2189130) says there's a C++20 proposal for C-style designated initializers, but its not the same as C# – kmdreko Jul 16 '18 at 16:51
  • 5
    That is not valid Java. – user2357112 Jul 16 '18 at 16:55
  • 3
    How is this any any way superior to using a constructor: `new StudentName( "Craig", "Playstead" )` ? –  Jul 16 '18 at 17:01
  • 3
    @NeilButterworth I think your example already showcased that. In the case where a class is only a bag of variables, and especially if they have common types, having a name for them is nice. How did you know it _wasn't_ `new StudentName("Playstead", "Craig")`? – Passer By Jul 16 '18 at 17:14
  • @Passer None of my classes are "bags of variables". And how did you know that it wasn't `FirstName = "Playstead", LastName = "Craig"`? Both have just as much type-checking. –  Jul 16 '18 at 17:18
  • 2
    @NeilButterworth I meant when type-checking doesn't help you in preventing misordering the parameters, having names for them are nice. – Passer By Jul 16 '18 at 17:24
  • @Passer Maybe it's just me - but I have to say for me that it is a non-problem. I'm not boasting, but I really cannot remember a case where I got the parameters of the same type reversed. Of course, it's maybe not harmful to have this feature added, but I tend to think that C and C++ should diverge. For example, VLAs should never be part of C++ (or C, IMHO), –  Jul 16 '18 at 17:33
  • @NeilButterworth It depends on the circumstance. Consider if you were using a date library, and you write `Date{day, month, year}` to construct a Date, where the variables are integers. Can you be sure that's correct? Why shouldn't it be `Date{month, day, year}` or `Date{year, month, day}`? It's easy to get wrong, and it's easy to miss if you get it wrong – Justin Jul 16 '18 at 17:36
  • @Justin I would argue it's just as easy to get it wrong and miss it with named parameters. Unless the compiler can spot it wit a strong type-system, all this really does is add more necessary complexity to the language without providing any type safety. And now I have to go and shout at a squirrel that is attacking my bird feeder. –  Jul 16 '18 at 17:42
  • "That is not valid Java." two points for C# – Bimo Jul 17 '18 at 19:15

1 Answers1

5

Take a look at designated initializers. They've been around in C since C99 but weren't added to C++. Now they are planned to be added in C++20, with some limitations though. Example:

struct db_config
{
    std::string host = "localhost";
    std::string port = "5432";
    std::string dbname;
    std::string user;
    std::string password;
} config = {
    .dbname = "test",
    .user = "admin",
    .password = "v3ry$3cur3"
};

Some compilers however (e.g. GCC) already allow the use of C designated initializers in C++ code as an extension.

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34