Reputation: 2265
I'm trying to run CPPReference's regex_search example:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string lines[] = {"Roses are #ff0000",
"violets are #0000ff",
"all of my base are belong to you"};
std::regex color_regex("#([a-f0-9]{2})"
"([a-f0-9]{2})"
"([a-f0-9]{2})");
for (const auto &line : lines) {
std::cout << line << ": "
<< std::regex_search(line, color_regex) << '\n';
}
std::smatch color_match;
for (const auto &line : lines) {
std::regex_search(line, color_match, color_regex);
std::cout << "matches for '" << line << "'\n";
for (size_t i = 0; i < color_match.size(); ++i)
std::cout << i << ": " << color_match[i] << '\n';
}
}
This program compiles with Clang++ 3.4-1ubuntu3 and GCC 4.8.2, but running it immediately gives this error:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core dumped)
This question indicated that the problem was one with GCC, but compiling with Clang produces the same problem. Even explicitly passing in the library at each step with the command:
clang++ -stdlib=libstdc++ -std=c++11 -H -c regex.cpp -o object.o && clang++ -stdlib=libstdc++ object.o -o regex
causes the error at runtime, despite the fact that the last lines of the first command's output are:
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_constants.h
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_error.h
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_cursor.h
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_nfa.h
... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_nfa.tcc
.... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/regex
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_compiler.h
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_grep_matcher.h
... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_grep_matcher.tcc
.... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/regex
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex.h
...which would indicate that the appropriate headers are present on my system.
Upvotes: 2
Views: 430
Reputation: 50101
You still use the incomplete gcc
standard library implementation, you even explicitly tell clang
to use it with -stdlib=libstdc++
.
The standard library implementation that comes with clang
is libc++
, you thus need -stdlib=libc++
instead.
Upvotes: 5