A Compiler Bug

Today, I was trying to validate some user input with C++11's new regular expressions. In essence, I tried the following.

auto r = std::regex("[0\\- ]");
std::cout << std::regex_match(some_string, r) << std::endl;

The code should compile, and does compile. However, the first line throws an error.

Invalid range in bracket expression.
terminate called after throwing an instance of 'std::logic_error'

This is somewhat surprising. The intent is to generate a (somewhat useless) regular expression checking if a string contains one of the three characters 0, -, or . Normally, the hyphen is used in bracket notation to denote a range; but this functionality can be deactivated in ECMAscript mode by escaping it with a back slash (which itself needs to be escaped). I actually crosschecked this with my browser and some regex-validators. The above regex should work. The only reason it doesn't is that there is a bug in GCC's standard library libstdc++. This means that the bug also appears when the code is compiled with Clang! However, it works with libc++ the llvm implementation of the STL. The bug is already fixed in trunk and should be gone by the next release. In the mean time the following works.

auto r = std::regex("[0 \\-]");

Small difference, big effect.