Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constexpression subscript operator of STL containers

constexpr const_reference at( size_type pos ) const;

How can this overload of STL container accessors work with non-constexpr parameters? What are classical use cases of this overload?

like image 564
Meteorhead Avatar asked Apr 11 '26 13:04

Meteorhead


1 Answers

There is no such thing as constexpr parameters in a function declaration. A constexpr function call can only be evaluated at compile time if and only if all parameters involved in that function call are constant expressions.

That being said, the only case where a constexpr function must be evaluated at compile time is when it is used to calculate a template parameter.

At least one use case for the example you give, is for std::array::at.

like image 74
rubenvb Avatar answered Apr 15 '26 17:04

rubenvb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!