为什么STL实现是如此不可读?如何C ++可以改进这里? [英] Why STL implementation is so unreadable? How C++ could have been improved here?

查看:93
本文介绍了为什么STL实现是如此不可读?如何C ++可以改进这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,为什么STL实现中的大多数成员都有 _M _ _ __ 前缀?
为什么有这么多的样板代码?

For instance why does most members in STL implementation have _M_ or _ or __ prefix? Why there is so much boilerplate code ?

C ++缺少什么特性使make vector(例如)实现清晰,更简洁? >

What features C++ is lacking that would allow make vector (for instance) implementation clear and more concise?

推荐答案

实现使用以下划线开头,后跟大写字母或两个下划线,以避免与用户定义的宏冲突。这样的名称在C ++中保留。
例如,可以定义一个名为 Type 的宏,然后 #include< vector> 。如果向量实现使用键入作为模板参数名称,它会破坏。
但是,不允许定义名为 _Type (或 __ type type __ 等)。因此,向量可以安全地使用这样的名称。

Implementations use names starting with an underscore followed by an uppercase letter or two underscores to avoid conflicts with user-defined macros. Such names are reserved in C++. For example, one could define a macro called Type and then #include <vector>. If vector implementations used Type as a template parameter name, it would break. However, one is not allowed to define macros called _Type (or __type, type__ etc.). Therefore, vector can safely use such names.

这篇关于为什么STL实现是如此不可读?如何C ++可以改进这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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