为什么 STL 的命名约定使用这么多前导下划线? [英] Why does the naming convention of STL use so many leading underscore?

查看:45
本文介绍了为什么 STL 的命名约定使用这么多前导下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,

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);
    }
}

既然这些模板都放在命名空间 std 中,并且大写,小写和大写都使用,为什么需要前导下划线(单双)?它只是那些开发人员的味道吗?还是有一些合理的技术原因?

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?

推荐答案

考虑如果标准库实现看起来像这样,使用不带下划线的合理名称会发生​​什么:

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>

comp 宏定义会导致预处理器将 sort 声明变成这样:

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)));
             ^^^^^^^^^^^

这将无法编译.因为允许用户定义宏的名称类似于 compCompareRandomAccessIterator,并且因为宏不尊重命名空间或作用域或其他上下文,标准库必须使用带有前导下划线的丑陋名称以避免冲突.这是安全的,因为禁止用户声明任何类似 __comp 的名称,因此不会发生冲突.此类名称称为保留名称.

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.

这篇关于为什么 STL 的命名约定使用这么多前导下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆