-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wpimath, wpiutil] Add wpi::array for compile time size checking #3087
Conversation
The wpimath APIs use std::array, which doesn't do size checking. Passing an array with the wrong size can result in uninitialized elements instead of a compilation error.
template <typename T, size_t N> | ||
class array : public std::array<T, N> { | ||
public: | ||
array() = default; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this let {} pass successfully even for a N-sized array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea... in that case, we need to fix the places we use the default constructor of std::array/wpi::array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a clean way of not using the default ctor in SwerveDriveKinematics.inc.
https://github.com/wpilibsuite/allwpilib/blob/master/wpimath/src/main/native/include/frc/kinematics/SwerveDriveKinematics.inc#L41-L53
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a constructor tag to explicitly request an unfilled array? E.g.
struct empty_array_t {};
constexpr empty_array_t empty_array = empty_array_t{};
class array {
public:
explicit array(empty_array_t) {}
That forces the empty case to be explicitly constructed as array<X,Y> arr(empty_array)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach, but it is rather verbose.
wpi::array<SwerveModuleState, NumModules> moduleStates{
wpi::array<SwerveModuleState, NumModules>::empty_array};
It can't see the definition of empty_array without the extra qualification. We could move the type tag definition outside the class. Ah, the snippet changed. :)
If you put the empty_array struct outside the class (see my edited comment), you can avoid the redundant template info. |
The wpimath APIs use std::array, which doesn't do size checking. Passing
an array with the wrong size can result in uninitialized elements
instead of a compilation error.