受保护变量的命名和标准
Protected variables naming and the standard
我偶然看到一篇介绍可迭代队列的文章。OP在实现中使用了来自std::queue
的受保护变量c
。
这是完全有效的吗?这个变量是否在所有实现中都具有相同的名称?也就是说,标准是否明确规定这个变量必须命名为c
?
为供参考,这里列出了std::queue
的确切定义。所以在回答
换句话说,标准是否明确规定这个变量必须命名为
c
?
是的,在这种情况下(对于其他容器适配器也是类似的);
template <class T, class Container = deque<T>>
class queue {
protected:
Container c;
// ...
};
然而,一般来说,受保护的和私有的名字和成员的名字是不标准化的,因为类型并不是都被构建为派生的,并且实现是一个实现细节(并且不构成公共API的一部分);例如,std::vector
没有列出任何受保护的名称。
一些std
容器和类确实定义了protected
成员的名称,特别是想到了iostreams库——基本上是打算从这些类型派生出来的。
作为后续- 是否所有的编译器/库都使用c
?看起来,至少主流软件(libstdc++、libc++和MSVC)是这样做的。libstdc++很有趣,因为它在变量上包含了以下注释;
/** * 'c' is the underlying container. Maintainers wondering why * this isn't uglified as per style guidelines should note that * this name is specified in the standard, [23.2.3.1]. (Why? * Presumably for the same reason that it's protected instead * of private: to allow derivation. But none of the other * containers allow for derivation. Odd.) */ _Sequence c;
相关文章:
- 标准在哪里定义了可变变量可以改变
- 如何在C++中从标准输入中获取变量输入
- 如何通过标准字符串打印 ostream 变量的值?
- C++标准对局部变量的存储和分配有什么保证?
- 标准与显式自动推导变量声明
- 在C++标准中哪里说必须初始化 const 内置类型变量的定义
- 兼容的声明 __attribute__ ((节( ".abc.dfe" ))) 常量易失性 uint8 属性变量 = 0;- 符合MISRA标准
- 关于循环变量优化的标准合规行为是什么?
- 根据C++标准,int 变量是对象吗?
- 如何在C 中的标准堆栈中推动结构类型变量
- 非标准语法;使用"&"创建指向成员的指针。访问布尔变量
- 基对象和继承对象,用于将标准变量包装在C++中
- C 标准:DO命名空间constexpr变量具有内部链接
- 如何从标准向量前端读取并删除读取变量
- 用于定义公共变量静态常量整数的C++标准
- 对变量上的标准构造进行排序
- 我应该使用来自C++标准还是来自 Windows API 的条件变量?
- 创建一个TH2,其中包含给定仓中变量的标准偏差
- 变量模板和标准::enable_if
- 是否有C++标准类在作用域出口处将变量设置为值