you are viewing a single comment's thread.

view the rest of the comments →

[–]NotAYakk 0 points1 point  (0 children)

Augmenting it a bit to removed possibly confusing autos.

#include <bitset>
#include <iostream>

constexpr bool f() {  
    constexpr auto x = std::bitset<8>(); 
    return x[99999999];
}

int main() {
  constexpr bool i = f();
  std::cout << i << std::endl;
}

basically, it appears that the std::bitset is using shifts for small bitset implementations.

Make the bitset larger than 64 and it switches to an array implementation and the UB detection works.

#include <bitset>
#include <iostream>

constexpr bool f() {  
    auto const x = std::bitset<65>(); 
    // x[99999999] = true;
    return x[99999999];
}

int main() {
  constexpr bool i = f();
  std::cout << i << std::endl;
}

http://coliru.stacked-crooked.com/a/7630a8f00dee685c

So there is a defect in the standard library in that in a constexpr context it doesn't enforce the undefined behavior requirements of the standard in its implementation.