r/cpp • u/vickoza • Jun 08 '23
What are thoughts on removing regular expression from the standard library?
3
u/gnolex Jun 08 '23
Do you have a specific reason in favor of deprecation and removal of regular expression library from the standard? Because I don't. I use regular expressions from time to time so having that in C++ directly is very helpful. Besides, you can't remove something from the standard on a whim, even if it's something you don't personally like or use.
0
u/vickoza Jun 08 '23
I am not advocating the removal of regular expressions but rather it feels like regular expressions are an afterthought in C++. It also add more syntax for a C++ developer to remember to use and I believe it is not used in
import stdincludes importing the regular expressions library.1
u/gnolex Jun 08 '23
I wouldn't say they're an afterthought. But I can agree that they are poorly designed and very slow. Much like a lot of C++ functions for converting between different encoding types, like to and from UTF-8.
1
u/goranlepuz Jun 09 '23
C++ functions for converting between different encoding types, like to and from UTF-8
Huh, why would these be C++ and not system or ICU (C)?
18
u/johannes1971 Jun 08 '23
If we remove every feature that could be made better, we'll end up with an extremely lean standard library and a greatly reduced workload for the committee.
Here's a list of all the standard library features that are already perfect:
2
u/TheoreticalDumbass :illuminati: Jun 09 '23
you dont think unique_ptr is perfect? how could it be possible to improve it?
6
u/johannes1971 Jun 10 '23
You could pass it in a register, instead of on the stack.
It's debatable whether ABI is really part of the language itself, of course...
1
u/TheoreticalDumbass :illuminati: Jun 10 '23
i get what you mean, but this seems outside of standard library scope, since it can't do anything about it :\
15
u/mallardtheduck Jun 08 '23
It's already well-used enough that even if "removed" from the standard, all the major compilers will have to keep it around for quite some time for compatibility with existing code.
What's the motivation to remove it?
14
u/2uantum Jun 08 '23
What? Why would it be removed? Am I out of the loop?
11
u/burntsushi Jun 08 '23 edited Jun 08 '23
See: https://old.reddit.com/r/cpp/comments/y2kv53/deleted_by_user/is54mze/
See: https://old.reddit.com/r/cpp/comments/e16s1m/what_is_wrong_with_stdregex/
And yeah, according to some you are out of the loop for a long time now, although none of the docs for
std::regexseem to warn you about this. So I'm not really sure how you're supposed to know.
20
u/MeTrollingYouHating Jun 08 '23
Why? I know it's not the fastest implementation but it works and often I don't really care about performance enough to deal with the hassle of an external library
8
3
u/dustyhome Jun 08 '23
Regular expressions are a general enough tool that it makes sense for it to be in the standard. I've found it to be useful on various occasions. I've heard there are issues with it, but nothing that affects me in my day to day work. If I needed a high performance one, I would look for a library, but I haven't seen the need yet.
I wouldn't mind having to recompile or rewrite my code slightly to support a version that fixed whatever issues people have with it, but I would not like to see it removed.
3
2
u/kammce WG21 | πΊπ² NB | Boost | Exceptions Jun 08 '23
Remove? Probably not. Even though the implementation is awful and the slowest regex library around, there are probably users who use it without caring about performance or are not knowledgeable about the performance issues.
One option is to improve the implementation, Which I heard may not be possible because of how the API was made. Another option is to make another version of regex to fix the issues with the current library.
And as with the second option, there's been a discussion of a std2 where many of the problems with the standard library, including certain API choices can stay in the normal standard library, but there's a new one that's better performance, lower memory, cost and fixes any particular issues that the previous standard library had. I'm in favor of this but the C++ committee doesn't seem to like this.
That's my two cents.
2
u/ReinventorOfWheels Jun 08 '23
An alternative implementation should be added that improves and solves the problems of <regex>. Then, some time later, the "old" one can be deprecated.
2
u/KiwiMaster157 Jun 08 '23
I don't have the numbers to back this up, but from personal experience, MSVC's implementation of std::regex isn't that bad. When people say std::regex is slow, they're usually talking about GCC's implementation.
2
u/Zeh_Matt No, no, no, no Jun 08 '23
Why would anyone even ask this?
2
u/operamint Jun 08 '23
There are suggestions that <regex> should be replaced by the high performance ctre implementation: https://github.com/hanickadot/compile-time-regular-expressions
2
u/Zeh_Matt No, no, no, no Jun 08 '23
Replacement != Removal
1
u/operamint Jun 08 '23
I think they aim for deprecating the current, i.e removing it in the end. It will still be around for long. The managed to get rid of auto_ptr, remember. I only refer to what I have read, so nothing confirmed here.
1
u/Zeh_Matt No, no, no, no Jun 08 '23
Right, but the op specifically asks for the removal. I think it would be best to make the current API better and more performant and perhaps add more constexpr/consteval things to it which then also keeps legacy code working, I'm not quite convinced that we have to just remove the current thing to replace it with another, the API is mostly ok it just seems to be the performance that people complain about.
1
u/operamint Jun 09 '23
Performance and compilation time. I believe ctre also can deal with non-const regexes, so it should be possible to create a std::regex compatible api on top of it. But I have the impression that the std::regex api itself is not much loved by the community, although I think it's workable too.
1
u/Zeh_Matt No, no, no, no Jun 09 '23
Fair point, I would however still try to see how much can be improved first before jumping to the conclusion that its a dead end and quite frankly when you are dealing with regex and performance is important then it might be not the best approach to the problem to begin with but that is just my opinion on that.
0
-2
u/vickoza Jun 08 '23
I personally do not care as long as it does not violate C++ goal of "you do not pay for things you do not use" as I do not use regular but I thing regular expression syntax add a second syntax to remember to use effectively.
8
u/johannes1234 Jun 08 '23
regular expression syntax add a second syntax to remember to use effectively.
Learning regexp is useful very independent from C++ offering it. Your editor quite certainly has a search with regexp, which is often useful. Tools like grep, sed, awk are useful. Basic understanding of regexp I would consider base knowledge for any software developer, whatever the language, whatever the domain one is in.
It's just the industry standard default way to search through strings, besides trivial search. Even when using a more specialized parser there likely are aspects of regexp in there.
For C++ the only thing is that std::regexp is slower than it could be, but dropping without replacement isn't a sensible option.
2
u/Logical_Put_5867 Jun 08 '23
Common features most similar languages share are important to make available, accessible, and easy to use, as many of these define the adoption of the language long term. Regex definitely falls in that category for me.
A new user should be able to minimally implement a standard regex without having to sort through and link 3rd party libraries. If these features are hard to find and use them place a barrier for new users, who are more likely to go around it by using a different language.
That being said, the current version is a bit clunky compared to other popular languages, and could use some improvement.
2
u/br_aquino Jun 08 '23
So you write a lot of code that you could do in half a line because you don't want to learn? And what you do if you find a piece of regex on someones code, do you ignore without understand it?
1
u/ioctl79 Jun 08 '23
You pay for every feature of the standard library and language. The cost may not be in runtime, but it is in cognitive overhead for you as a programmer and maintenance burden for compiler/library developers.
1
u/Zeer1x import std; Jun 08 '23
Everyone who learned computer science should know regular expressions. Every other major language has regular expression parsers too.
I use <regex> for command line parsing (together with Boost.ProgramOptions), it is easily fast enough for that.
When the need for a faster library comes, you could switch to Boost.Regex, which should have the same interface, or any other library.
-7
Jun 08 '23
well i think it kind of makes sense, as regex arent actually c++, they are more like a separate library defaulted in c++. And I think in most cases where you need to work with regex youll be using other language, like python, c#, ls, etc
7
u/dustyhome Jun 08 '23
You'll be using another language because C++ doesn't have them. Kind of a circular argument.
4
u/Logical_Put_5867 Jun 08 '23
Regex aren't pure c++ sure, but if you remove all convenience libraries from the standard that aren't "pure" there's going to be a lot missing.
The goal of the c++ standard should not be to tell people to use other languages. The goal should be to make those features just as convenient to use so that c++ can be a goto language for these applications as well (so long as other priorities can be maintained, but optional libraries shouldn't cause some huge hit to runtimes or anything).
27
Jun 08 '23
[removed] β view removed comment
3
u/deeringc Jun 11 '23
They should just add regex2, regular_expression or something like that. Look at auto_ptr - just because that was a mistake it didn't stop better replacements like unique_ptr and the eventual deprecation of the old flawed design. I find it bizarre that this hasn't already happened for regex.
4
u/elperroborrachotoo Jun 08 '23
Where does that question come from?