3

I'm trying to utilize the 'TR1' regular expression extensions for some C++ string parsing. I've read that the <regex> header and namespace std::tr1 are required for this

I can compile with the <regex> header present(though it forces me to use either the flag, -std=c++0x or -std=gnu++0x)

However, when I attempt to use the std::tr1 namespace in my program, compiling fails with message that tr1 "is not a namespace name". I cant do things like,

std::tr1::regex rx("mypattern");

I've read that TR1 regular expressions have been supported since gcc 4.3.0. I'm using g++ through gcc 4.4.5.

Am I missing something?

rocketas
  • 1,679
  • 3
  • 18
  • 30

1 Answers1

8

g++ 4.7 doesn't implement regular expressions yet.

But despite that fact, in C++11 regex has been moved from the namespace std::tr1 to std. So, instead of std::tr1::regex, you should write std::regex:

std::regex rx("mypattern");

I don't know for which g++ versions before 4.7 this applies, too. But this ideone example compiles fine with g++ 4.7. However, remember that the regex implementation isn't implemented in this compiler version.

leemes
  • 44,967
  • 21
  • 135
  • 183
  • It is still a largely empty implementation in G++. – juanchopanza Feb 12 '13 at 18:55
  • Yeah, it compiles but doesn't do hardly anything. – Seth Carnegie Feb 12 '13 at 18:56
  • @juanchopanza Thanks, I'll add this. Didn't know and just did a quick check and didn't notice that. – leemes Feb 12 '13 at 18:56
  • 6
    I feel uneasy about the fact that there is `` but unimplemented. It compiles but behaves as nonsense. Pretty dangerous if you don't know that. – leemes Feb 12 '13 at 19:04
  • 2
    @leemes - welcome to the world of free software. You get what you pay for. – Pete Becker Feb 12 '13 at 20:52
  • It's not "dangerous" unless you write code and don't bother testing it, in which case it's you that's dangerous, not the C++ implementation. See http://stackoverflow.com/a/12665408/981959 for why a header is shipped with an incomplete implementation – Jonathan Wakely Feb 14 '13 at 00:33