mardi 22 mai 2018

c++11 For structs is there an automatic way to initialise a new variable that does not involved writing a constructor?

I think that I vaguely recall that one of the newer c++ standards (maybe its c++11, or maybe 14?...17??) allows you to initialise a struct, whereby you can define a struct and then initialise it without having to write a constructor.

E.g.:

struct test
{
    int a;
    int b;
    std::string str;
}

int main()
{
    std::map<int, test> test_map;
    test_map[0] = test(1, 2, "test1"); // This is the line in question
    // Or it might be more like: test_map[0] = test{1, 2, "test1"};
    return 0;
}

I can't recall the name of this special initialisation (or if it even exists)!. So my questions are:

  • Is there some new mechanism to achieve this without writing a constructor in the struct "test"?
  • If so, what is it called (so I can read more about it).

If this "feature" does not exist then please put me out of my misery!, it could be that my imagination has made this up...

Aucun commentaire:

Enregistrer un commentaire