r/cpp • u/javascript • May 05 '26
Strategies for *requiring* designated initializers when constructing a type?
Given some aggregate like this:
struct InitParams {
int size = 0;
int capacity = 0;
};
And given that it is used in some factory function like this:
auto Make(InitParams ip = {}) -> std::optional<MyClass>;
I want to design the aggregate type to allow callsites like...
Make({ .size = 10, .capacity = 20 });
Make({ .size = 10 });
Make({ .capacity = 20 });
Make({});
Make();
But I also want to reject callsites like...
Make({ 10, 20 });
Make({ 10 });
Make({ 20 }); // Oops! This is the size, not the capacity!
I was hoping that reflection would unlock the ability to specify this on the type alone somehow, but I've been unable to figure out how to do it.
I can approximate this behavior using an overload set where a similar-but-different param type is accepted by a different Make overload but then that would result in default construction being ambiguous too. You can see that in action here:
struct DesignatedInitRequired {
int DO_NOT_SPELL_THIS_FIELD_NAME0;
int DO_NOT_SPELL_THIS_FIELD_NAME1;
};
auto Make(DesignatedInitRequired dir = {}) -> void = delete("Use designated init");
Make({ 10 }); // Ambiguous, ill formed (which is what I want)
Make({ 10, 20 }); // Ambiguous, ill formed (which is what I want)
Make({}); // Ambiguous, ill formed (which is NOT what I want)
Make(); // Ambiguous, ill formed (which is NOT what I want)
So I could make something to reflect on InitParams and produce DesignatedInitRequired, but that wouldn't be enough. Every function that accepts InitParams would need an overload for DesignatedInitRequired meaning that it becomes a property of the type AND its users, not just the type itself.
Any ideas on how to achieve my goal?
EDIT: I think this is a sufficient solution! https://godbolt.org/z/h6sbcaTj4
I thanked the person that told me but then they deleted their comment. Anyway...
23
u/chengfeng-xie May 05 '26 edited May 05 '26
I think the closest implementation that follows your examples is to use the following trick (inspired by Barry and jbandela; CE):
17
u/argothiel May 05 '26
I would strongly recommend different types, if you want them not to be confused. That way, you'd have
Make(Size(10), Capacity(20))orMake{Size{10}, Capacity{20}}.6
u/Rabbitical May 05 '26
I use linting for this, clang warns me to use designations. I get that's not the same as "won't compile", but at the point that you're trying to use reflection to solve this you're basically reinventing static analysis.
I guess you could add a trick first member that doesn't take numbers which would make raw values fail but that's not something I would ever do.
6
u/Jakabxmarci May 05 '26
We use clang-tidy for this
4
u/johannes1971 May 05 '26 edited May 05 '26
You could use dedicated types:
1
u/mjklaim May 05 '26
I don't have a need for such thing, but one way to do it would be to use type wrappers that do not provide a default constructor: (pseudocode)
If you have access to reflection, maybe something can be done through generating a constructor based on the members of the params struct which are marked with an annotation.
1
u/Sopel97 May 06 '26
the builder pattern/named constructors
-5
u/sweetno May 05 '26
C++ doesn't have named arguments. All ways to emulate them will be much code and maintenence burden. Just accept it and move on.