Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the naming convention of STL use so many leading underscore?

Tags:

stl

For example,

template<typename _RandomAccessIterator, typename _Compare>
inline void
sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
     _Compare __comp)
{
    typedef typename iterator_traits<_RandomAccessIterator>::value_type
        _ValueType;

    // concept requirements
    __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
                                    _RandomAccessIterator>)
        __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
                                    _ValueType>)
        __glibcxx_requires_valid_range(__first, __last);

    if (__first != __last)
    {
        std::__introsort_loop(__first, __last,
                              std::__lg(__last - __first) * 2, __comp);
        std::__final_insertion_sort(__first, __last, __comp);
    }
}

Since those templates are placed within the namespace std and capital, lowercase and uppercase are all be used, why are leading underscore(single and double) needed? Is it just a flavor of those developers? Or are there some reasonable technical reasons?

like image 721
Yantao Xie Avatar asked Mar 11 '14 08:03

Yantao Xie


2 Answers

Consider what would happen if the standard library implementation looked like this instead, using sane names without the underscores:

template<typename RandomAccessIterator, typename Compare>
inline void
sort(RandomAccessIterator first, RandomAccessIterator last,
     Compare comp);

Now if that was included in a user's program which started like this:

#define comp(X, Y) ((X) < (Y))
#include <algorithm>

The comp macro definition would cause the preprocessor to turn the sort declaration into this:

template<typename RandomAccessIterator, typename Compare>
inline void
sort(RandomAccessIterator first, RandomAccessIterator last,
     Compare ((X) < (Y)));
             ^^^^^^^^^^^

This would fail to compile. Because users are allowed to define macros with names like comp and Compare and RandomAccessIterator, and because macros do not respect namespaces or scopes or other context, the standard library must use ugly names with leading underscores to avoid clashes. This is safe because it is forbidden for users to declare anything with names like __comp, so there can be no clash. Such names are called reserved names.

This is not a coding standard, it is the only way an implementation can prevent clashes with arbitrary user-defined names. For the convention to work users must never declare anything with a reserved name. You should absolutely not copy the convention for your own code.

like image 80
Jonathan Wakely Avatar answered Oct 14 '22 09:10

Jonathan Wakely


These names are reserved and are not allowed to appear in user code, see e.g. What are the rules about using an underscore in a C++ identifier?. By choosing these names in the standard document or its implementation name clashes can be avoided.

like image 38
Benjamin Bannier Avatar answered Oct 14 '22 10:10

Benjamin Bannier