I study constexpr. I can not understand why the compiler does not consider one of the expressions as constant, because it fits the requirements.
Code:
#include <iostream> #include <cstdlib> #include <array> constexpr int f( int x ) { if ( x < 0 ) x = 0; return ++x; } int main() { typedef double T; typedef std::array<T, 3> C; constexpr C c = {1, 2, 3}; constexpr C::const_reference ref = c.at( 0 ); // error: is not a constexpr static_assert( ref == 1, "ref != 1" ); constexpr int y = -3; //y = f( y ); static_assert( f( y ) == 1, "f( y ) != 1" ); return 0; } You can compile this link.
In the implementation of stl, with which I work for the array, the at function is declared as constexpr and the array has no constructors at all, the destructor is also not declared, which means the class has a destructor by default, the array itself is initiated, so I don’t know why it does not work.
Interested compilers gcc 5.4.0 and gcc 6.4.0. clang doesn't compile this either.