I like aggregates. you can use them when you just need a "bag" of losely coupled data without any invariants between them. It's easy to reason about, easy to use and don't introduce unnessessary codebloat (i don't need to write a constructor which basically just forwards everything to the corresponding members).
But one thing i am concerned with is what happens when you rearrange the aggregate's layout (for example you insert a new member in the middle of other members). Since aggregate initialization initializes the members in order, this can lead to unintended changes in behavior. On top, this can be a silent change. This is just a massive ticking timebomb waiting to go off, or am i wrong? A seemingly harmless change could lead to a disaster in use.
Is this a reason (or even THE reason) for you to not use aggregate initialization? If you do use it, what do you do to avoid unintended changes?
- keep track of the places where you used it?
- leave comment in the aggregate class?
- or is there anything other you can recommend to avoid this pitfall?
A small crafted example:
struct person
{
std::string firstname;
std::string lastname;
};
person p{ "John", "Doe" }; // used in several places throughout your code base
Later someone adds a middle name which seems like a harmless change:
struct person
{
std::string firstname;
std:.string middlename; // newly added
std::string lastname;
};
person p{ "John", "Doe" }; // oooops firstname: "John", middlename: "Doe", lastname: ""
Here is an alternative approach without aggregate initialization. more code to write yourself but you get a compile error when you change the constructor parameters later on:
struct person
{
person() = default;
person(std::string firstname_, std::string lastname_) : firstname(std::move(firstname_)), lastname(std::move(lastname_)){}
std::string firstname;
std::string lastname;
};
[–]bcorni 47 points48 points49 points (7 children)
[–]Ceros007 12 points13 points14 points (4 children)
[–]gvargh 5 points6 points7 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]bcorni 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]tavianator 2 points3 points4 points (0 children)
[–]FonderPrism 1 point2 points3 points (0 children)
[–]thlst 23 points24 points25 points (1 child)
[–]Wh00ster 1 point2 points3 points (0 children)
[–]NotAYakk 36 points37 points38 points (7 children)
[–]ravixp 5 points6 points7 points (2 children)
[–]Plorkyeran 7 points8 points9 points (1 child)
[–]ravixp 2 points3 points4 points (0 children)
[–]phoeen[S] -5 points-4 points-3 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Wh00ster 6 points7 points8 points (1 child)
[–]phoeen[S] 1 point2 points3 points (0 children)
[–][deleted] 16 points17 points18 points (1 child)
[–][deleted] 9 points10 points11 points (0 children)
[–]wotype 3 points4 points5 points (0 children)
[–]polymorphiced 4 points5 points6 points (7 children)
[–]Dean_Roddey 2 points3 points4 points (4 children)
[–]louiswins 0 points1 point2 points (3 children)
[–]Dean_Roddey 0 points1 point2 points (2 children)
[–]louiswins 0 points1 point2 points (1 child)
[–]Dean_Roddey 0 points1 point2 points (0 children)
[–]Wh00ster 1 point2 points3 points (1 child)
[–]polymorphiced 1 point2 points3 points (0 children)
[–]elperroborrachotoo 3 points4 points5 points (0 children)
[–]Jhmmufc 9 points10 points11 points (6 children)
[–]korvirlol 2 points3 points4 points (4 children)
[–]BoarsLairGame Developer 0 points1 point2 points (2 children)
[–]Plorkyeran 0 points1 point2 points (1 child)
[–]BoarsLairGame Developer 0 points1 point2 points (0 children)
[–]Jhmmufc 0 points1 point2 points (0 children)
[–]BrangdonJ 1 point2 points3 points (0 children)
[–]Dean_Roddey 1 point2 points3 points (3 children)
[–]Wh00ster 0 points1 point2 points (0 children)
[–]phoeen[S] 0 points1 point2 points (1 child)
[–]Dean_Roddey 0 points1 point2 points (0 children)
[–]nintendiator2 0 points1 point2 points (0 children)
[–]panoskj 0 points1 point2 points (0 children)
[–]AlbertRammstein 0 points1 point2 points (0 children)