Skip to content
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

Merged
merged 10 commits into from
Jan 17, 2021

Conversation

calcmogul
Copy link
Member

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.

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.
@calcmogul calcmogul requested review from PeterJohnson and a team as code owners January 14, 2021 19:51
@calcmogul calcmogul requested review from a team as code owners January 14, 2021 23:17
template <typename T, size_t N>
class array : public std::array<T, N> {
public:
array() = default;
Copy link
Member

@PeterJohnson PeterJohnson Jan 15, 2021

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?

Copy link
Member Author

@calcmogul calcmogul Jan 15, 2021

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.

Copy link
Member Author

@calcmogul calcmogul Jan 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@PeterJohnson PeterJohnson Jan 15, 2021

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)

Copy link
Member Author

@calcmogul calcmogul Jan 15, 2021

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. :)

@PeterJohnson
Copy link
Member

PeterJohnson commented Jan 15, 2021

If you put the empty_array struct outside the class (see my edited comment), you can avoid the redundant template info.

@PeterJohnson PeterJohnson merged commit f393989 into wpilibsuite:master Jan 17, 2021
@calcmogul calcmogul deleted the wpi-array branch January 17, 2021 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants