Personally, I'd skip std::array
/std::vector
here, because in your particular case, the position of each value imposes independent meaning. In general, sequence types have ordering, tuple
s have structure; if the element count is fixed (and often heterogeneous) and sorting (or otherwise reordering the values) is intrinsically nonsensical (e.g. in the case of a coordinate, swapping the x
and y
values changes the meaning), then a tuple
makes more sense.
In this case, you could just declare:
std::tuple<float, float, float> getPos() {
// C++17 or higher allows list initialization
return {x, y, z};
// Pre-C++17 you use the std::make_tuple helper
return std::make_tuple(x, y, z);
}
The advantage here is that you can then unpack the result in the caller easily, either with std::tie
:
float x, y, z;
std::tie(x, y, z) = a.getPos();
or on C++17 or higher with structured bindings, it's even nicer, since you can declare and initialize the variables with auto
, rather than declaring with explicit types, then reassigning with tie
:
auto [x, y, z] = a.getPos();
You can store the tuple
itself and use std::get
if you prefer, but unpacking to useful names rather than obscure std::get
indices usually makes for much cleaner code.