0

I'm wondering why C++ standard decided to make regex_* functions (regex_match, regex_search, regex_replace) non-member non-friend. They all need to access basic_regex's internals in order to perform the algorithms. Why don't they make them member functions of basic_regex? What are the benefits of free functions in this case?

4

1 Answer 1

1

Since the regex_* functions are non-member, non-friend, they only have access to the public interface of basic_regex. If they only need access to the public interface there's not much of a gain in being a member function over a free function, since both members and free functions can access a public interface. Also, by being free functions you remove dependencies that would connect the regex_* functions with the basic_regex class. If the regex_* functions are added to or altered or removed from, users of basic_regex that don't use those specific regex_* functions shouldn't have to recompile or bat an eyelash. This will help to future proof the clients of those functions and basic_regex for any changes that are dreamed up in future standards.

The regex_* functions would need to be member functions or friend functions only if they needed access to protected or private members of the basic_regex's interface.

Why should there be an added dependency if it is not needed?

Sign up to request clarification or add additional context in comments.

7 Comments

-1: They probably are friends -- there's no way for user code to tell if they are or not and the standard does not specify.
fair enough, but even as friends there is still a bit more freedom using the free functions over the member functions. The basic_regex class is no more dependent on the functions.
I don't think you can implement those regex_* functions using just the public interface of basic_regex. But if they are friend, then what member functions are for? I don't see any benefits of making them friend over members.
Non-member functions can be packaged separately from a class and changes to them can occur without affecting clients of that class. Member functions must be tied completely to a class, and any changes to any member functions will be felt at least as a recompile for all clients of that class.
In this particular case, I don't think anyone would use basic_regex alone without considering these associated algorithms. Aren't these "free functions" implicitly tied to the class?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.