microsoft / GSL

Guidelines Support Library
Other
6.11k stars 736 forks source link

how to do bound check for vector::front and vector::back #1141

Closed qqiangwu closed 8 months ago

qqiangwu commented 8 months ago

GSL.util provides at to bound-check std containers. But how can I force bound-check front() or back() for std containers. Are there any good practice?

I've written a clang-tidy checker to enforce bound-check operator[] operations, but I have no idea how to flag front or back.

beinhaerter commented 8 months ago

That is an interesting question.

I think it would be possible to implement free functions like template<typename Container> auto gsl::front(Container container) and template<typename Container> auto gsl::back(Container container) that Ensure(!container.empty()) and then forward to container.front() or container.back().

But from my understanding the GSL here only implements what the CppCoreGuidelines define/specify.

qqiangwu commented 8 months ago

I understand. Maybe I should make a fork and add such functions, and use the fork in my organization.