2

e.g.

#include <iostream>
using namespace std;


struct Point {
    int x;
    int y;
};

int main() {
    Point p1 {1};
    Point p2 {.y=2};

    cout << p1.x << ", " << p1.y << endl;
    cout << p2.x << ", " << p2.y << endl;
    return 0;
}

https://ideone.com/DlJRfU

Is there any way to force the caller to initialize all struct members when using initializer lists? I want to ensure both x and y are set by the caller in this example.

I also want to allow designated initializers. Many of our structs are very long and that syntax adds much-needed clarity.

9
  • 6
    Add a constructor to the structure? May 26, 2020 at 19:30
  • 2
    @mpen They are set to zeroes. May 26, 2020 at 19:30
  • Point p2 {.y=2}; compiles in GCC, but is not valid C++. May 26, 2020 at 19:37
  • 4
    @MooingDuck Designated initializers were added to C++20: en.cppreference.com/w/cpp/language/… May 26, 2020 at 19:38
  • 1
    Do you perhaps mean aggregate instead of struct? "struct" isn't really much of a thing in C++, other than an alternative keyword to declare/define classes. May 26, 2020 at 19:42

5 Answers 5

5

Something like what you want is possible in practice by making the aggregate meaninglessly templated:

template<class Bad>
struct PointImpl {
  int x=Bad(),y=Bad();
};
using Point=PointImpl<void>;

void f() {
  Point p0;             // error
  Point p1{1};          // error
  Point p2{.y=2};       // error
  Point p3{.x=1};       // error
  Point p4{.x=1,.y=2};  // OK
}

I won’t say I morally agree with this sort of “defeat the client” approach, and it’s not even clear from the standard that default member initializers should work like default arguments this way ([temp.inst]/3, /11, and /13 don’t mention them at all), but implementations agree that they do.

3
  • Woah, neat trick! Why does this work? I can't quite figure it out.
    – Catskul
    May 12, 2022 at 20:09
  • 1
    @Catskul: If you fail to supply an initializer for any member, it attempts to use the default member initializer, but that's ill-formed because it tries to initialize from void(). May 12, 2022 at 23:13
  • So the ill-formed-ness is kind of using SFINEA-like pattern, but it's just kind of hard to see since it's happening behind the scenes.
    – Catskul
    May 13, 2022 at 17:57
4

You could add a 2-parameter constructor. That would force initializing with both parameters.

Point(int x, int y) : x(x), y(y) {}
Point() = default; // to maintain behaviour of default constructor

then

Point p1{1, 2}; // OK
Point p1 {1}; // error: no matching constructor for initialization of 'Point'
Point p2 {.y=2}; // error: no matching constructor for initialization of 'Point'

Note that this means Point is no longer an aggregate, which means you can no longer use designated initializers.

3
  • No possible way to do it while still allowing designated initializers?
    – mpen
    May 26, 2020 at 19:51
  • @mpen I think not, but I am not up to scratch with C++20. The definition of aggregate changed a little, but I think my note still applies. May 26, 2020 at 20:01
  • 5
    C++ 20 requires no user-declared or inherited constructors so it would not be an aggregate. May 26, 2020 at 20:03
3

Sure, set them yourself. With

struct Point {
    int x = 0;
    int y = 0;
};

x and y will always be initialized. If you do not provide a value for any of the members, the default value (0 in this case) will be used.

2
  • 1
    The key to the question being "force the caller". I don't want default values. Default values cause bugs when new members are added.
    – mpen
    May 26, 2020 at 19:49
  • 1
    @mpen If you want to keep the class as an aggregate, AFAIK this is your only option. May 26, 2020 at 19:51
2

You declared an aggregate

struct Point {
    int x;
    int y;
};

If a member of an aggregate is not initialized explicitly using an initializer list, it is initialized as an empty pair of braces is used to initialize it. That is, this initialization:

Point p1 {1};

is equivalent to:

Point p1 {1, {}};

that means that the data member y will be zero-initialized.

If you want to provide that data members of an aggregate would be initialized even when the user did not use a list initialization, you can use default initializers, for example:

struct Point {
    int x = 0;
    int y = 0;
};
1

What you want is impossible in the language. In C++, a type cannot simultaneously force an initialization invariant on the user (like 'you must explicitly initialize all members') and be an aggregate (a type whose component objects can be initialized with any legal value of that type). If you want a type to be an aggregate (and therefore permit aggregate initialization), then by definition you are OK with a user implicitly value initializing some of the subobjects of that type. If you want a type to be initialized in a certain, very specific way, then it can't be an aggregate.

Not the answer you're looking for? Browse other questions tagged or ask your own question.