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?
1 Answer
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?
7 Comments
friends -- there's no way for user code to tell if they are or not and the standard does not specify.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.friend, then what member functions are for? I don't see any benefits of making them friend over members.basic_regex alone without considering these associated algorithms. Aren't these "free functions" implicitly tied to the class?