Consider the following code:
#include <bitset>
#include <sstream>
#include <iostream>
int main(int argc, char* argv[])
{
std::stringstream stream;
std::bitset<1> bitset(1);
std::cout<<"before = "<<bitset[0]<<std::endl;
stream<<"4";
stream>>bitset;
std::cout<<"after = "<<bitset[0]<<std::endl;
return 0;
}
Compiled under g++
with libstdc++
, the result is:
> g++ bitset_empty.cpp -o bitset_empty
> ./bitset_empty
before = 1
after = 1
Compiled under clang++
with libc++
, the result is:
> clang++ -stdlib=libc++ bitset_empty.cpp -o bitset_empty
> ./bitset_empty
before = 1
after = 0
Which one is right? Both (because of undefined behavior)? GCC? Clang?