If we take a look at GNU's implementation of libstdc++, I've noticed that in the implementations of the standard classes that private member functions of various classes are prefixed with _M_
. For example, std::basic_string<>
has among others a member called bool _M_is_shared() const;
.
I understand the motivation to have some sort of naming convention for private member variables. This helps is distinguishing between class members and function local variables visually. But I don't get why the _M_
prefix is preferred for private member functions.
If I see some code that called for example: is_shared();
there is essentially only a few options:
- it's a member function of this class
- it's a member function of a parent class
- it's a global function.
The first two, would both have the prefix so it's no help. The last one wont happen in any sane implementation because of namespace pollution concerns. The only globals the library should introduce are ones prescribed by the standard. So here's the crux of the question...
Since private member functions aren't publicly accessible. Can't effect derived classes in any way. I don't think that name collisions are really a concern here... and basically these are nothing more than a private implementation detail. Why bother with the (IMO) ugly _M_
prefixing? Is there something in the standard that disallows introducing extra private members? If so that would strike me as silly unless there is something I am missing.
__foo
for locals and arguments, and_M_foo
or_S_foo
for members, but in both cases we have to use reserved names to ensure we don't clash with any macros defined by users. – Zellers