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++c libstdc++, the result is:
> g++ bitset_empty.cpp -o bitset_empty
> ./bitset_empty
before = 1
after = 1
Compiled in clang++s libc++, result:
> clang++ -stdlib=libc++ bitset_empty.cpp -o bitset_empty
> ./bitset_empty
before = 1
after = 0
Which one is right? Both (due to undefined behavior?)? Gcc? Clang?
source
share