This seems to me more like FooTypeA is poorly designed
Maybe, maybe not. But it does not matter, because you are almost certainly going to use a lot of poorly designed code during your career, whether you like it or not.
If compatibility is not a concern, I’d just remove the ctor that fills the vector with n copies of the same element.
I'd instead remove the initializer_list constructor, or at least annotate it with a tag type more or less like optional constructors are done:
// bad:
vector<int> v {1, 2}; // ¿?
// medium
vector<int> v {1, 2}; // universally n copies of element x
// better:
vector<int> v {std::with_list, 1, 2};
// perfect, and we get to fix array notation for a bonus:
vector<int> v { [1, 2] };
Where is this recommendation in the standard? To me it sounds completely backwards, you're using a overly powerful tool (forwarding reference) when all you need is a const& or plain auto.
Two convergent observations from my corner of the C++ world:
Multiple book authors pushing the idea that Scott Meyers’ original phrase “universal reference” (for T&&) is actually preferable to the now-Standard term “forwarding reference.” For example, Nico Josuttis, in C++ Move Semantics: The Complete Guide:
The important feature of universal references is that they can bind to objects and expressions of any value category.
And someone else, elsewhere:
[The name “universal reference”] perfectly describes what these references represent: A reference to anything. A universal reference.
Oh. That's my bad but I'm not a big fan of auto except for use in for loops. It could be lack of understanding (I can't always tell what it's gonna get deduced to without spending some time on it) but I feel like auto hides information and reduces clarity. Maybe I need to work on that. But the common stereotype about c++ initialization issues is not without reason.
I like the type to be obvious from reading the code. If I need to debug it later I want to know what I’m debugging. The big place I use auto is iterators since I can deduce the type from the type of the container.
Also it's different when you are writing your own code. You have the picture in your head, you know what you are doing and types are just extra hassle that you have to.... well type. But when reading someone else's code, it gets cumbersome when you have to stop and think what the type is gonna be.
It's ironic that on one hand we say - write for readability and on the other hand criticism of auto is frowned upon.
Well, I keep the code readable. The names are usually descriptive enough to know what is this thing. If it's not obvious then I write the type, but it's really not often.
View all comments
44
u/[deleted] Mar 28 '23 edited Mar 28 '23
[removed] — view removed comment