Why is C ++ 11 building new functions (stod, stof) and not member functions of the string class?

Why are these C ++ 11 new <string> header functions ( stod , stof , stoull ) not member functions of the string class?

Is C ++ suitable for writing mystring.stod(...) rather than stod(mystring,...) ?

+7
c ++ c ++ 11
source share
3 answers

This is a surprise for many, but C ++ is not an object-oriented language (unlike Java or C #).

C ++ is a language with several paradigms and, therefore, tries to use the best tool to work when possible. In this case, the correct function is the free function.

Recommendation : Prefer non-member functions for member functions (from Efficient C ++, Item 23)

Reason : a member function or a friend function has access to inner classes, while a function other than a member is not used; therefore, using a non-member non-member function increases encapsulation.

Exception : when a member function or a familiar function provides a significant advantage (for example, performance), then it is worth considering, despite the additional connection. For example, even if std::find works very well, associative containers such as std::set provide a member function std::set::find that works in O (log N) instead of O (N).

+22
source share

The main reason is that they do not belong there. They actually have nothing to do with strings. Stop and think about it. User-defined types must follow the same rules as built-in types, so every time you define a new user type, you need to add a function to std::string . This is actually possible in C ++: if std::string had a member of the to function template, without a common implementation, you can add specialization for each type and call str.to<double>() or str.to<MyType>() But is it really what you want. This does not seem like a clean solution for me, anyone writing a new class to add the specialization std::string . Putting this kind of thing into a string class is his bastard, and in fact it is the opposite of what OO is trying to achieve.

If you insisted on pure OO, they would have to be members of double , int , etc. (the constructor, really. this is what Python does, for example.) C ++ does not insist on pure OO and does not allow the use of basic types like double and int for have members or special constructors. Thus, free functions are both an acceptable solution and a single pure solution is possible in the context of the language.

FWIW: Converting to / from a text view is always a delicate problem: if I do this in the target type, then I have introduced a dependency on various sources and absorption of text in the target type --- and they can change over time. If I do this type of source or receiver, I make them type dependent, even worse. The C ++ solution is to define a protocol (in std::streambuf ) where the user writes a new free function ( operator<< and operator>> ) to handle conversions and expects permission to overload the operator to find the right function. The advantage of the free solution function is that the transformations are not part of either type data (which, therefore, should not know about sources and sinks), or the type of source or receiver (which, therefore, should not know about user-defined data types ) This seems like the best solution for me. And functions like stod are just handy functions that make one particularly frequent use easier to write.

+3
source share

These are actually some utility functions, and they do not have to be inside the main class. Similar utility functions, such as atoi, atof, are defined (but for char *) inside stdlib.h, and they are also standalone functions.

+1
source share

All Articles