在GNU编译器中,typedef的名称查找有缺陷
name lookup for typedef is buggy in GNU compiler?
以下代码
#include <iostream>
typedef double A; // a global typedef
template <class Z> struct B // a template class...
{
A i{22.2}; // global typedef is in scope
typedef int A; // now a local typedef with the same name is introduced
A b{24}; // now the local typedef is in scope
Z c{36}; // a simple member of the template type
};
template <class Z> struct C : B<Z> // a template struct inheriting B
{
A a; // global typedef is in scope because we are in a template struct
C( ) : a(2.2){ }
};
int main( )
{
C<int> c;
std::cout << "c's members: "
<< c.a << ' '
<< c.i << ' '
<< c.b << ' '
<< c.c << std::endl;
std::cout << "their sizeof: "
<< sizeof(c.a) << ' '
<< sizeof(c.i) << ' '
<< sizeof(c.b) << ' '
<< sizeof(c.c) << std::endl;
}
不是由GNU-g++ 4.9.2
编译的,而它是由clang 3.5.0
编译的,它的行为正如我在嵌入的注释中试图解释的那样,正如它可以从生成的输出中看到的那样。这是GNU编译器中的一个错误吗?诊断显示typedef int A;
线在struct B
错误:从"typedef double A"更改"A"的含义
注意,当层次结构不是由template
组成时(当然,Z c{36};
声明被删除),由clang
在C
的范围内执行的查找(正确地,正如我所认为的)在B
的范围内找到typedef
,并认为成员a
是CCD_ 12型;则发出初始化CCD_ 13常数CCD_。。。
来自c++标准草案(N4140)
§3.3.7【基本范围类别】
2) S类中使用的名称N在其上下文中以及在S的完整范围内重新评估时应指代相同的声明。违反此规则不需要进行诊断。
CCD_ 15最初指的是全局CCD_。但在声明B::A
之后,当在B
的已完成范围中重新评估时,它将引用B::A
。这违反了上述规则。
要修复此问题,请使用完全限定名称:::A i{22.2}
。即使在声明了B::A
之后,::A
也始终引用全局A
,因此不违反规则。
这不是g++中的错误;这只是一个格式错误的程序。编译器不需要为您提供违反规则的诊断,但也不需要接受它。
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 当子类需要在 c++ 中相互包含时,继承有缺陷
- 无法打印完整的二叉搜索树,因为我从最低节点向后迭代的逻辑有缺陷
- 查找有问题的宏
- 关于模板参数推理的看似有缺陷的段落?
- 尝试在C++中"blur"矩阵;有缺陷的算法或代码?
- 链接列表,我的逻辑有缺陷
- 在C++中,这个Eigen::张量的声明是安全的,还是有缺陷的?我应该为它提交一个问题吗
- 否则字符逻辑有缺陷
- Qt自定义树模型显示正确,但有缺陷且速度慢
- 有缺陷的 DLL(使用外部函数)
- boost asio-更改有缺陷的代码
- 在GNU编译器中,typedef的名称查找有缺陷
- 支持C++11的有缺陷编译器版本的存储库
- 双链表插入项算法有缺陷
- c++蟑螂项目.消灭功能有缺陷
- visual C++:使用条件语句的控制台输出有缺陷
- 我使用标准库的查找有什么问题C++?