【发布时间】:2016-02-07 18:56:07
【问题描述】:
以下代码
#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 编译的,其行为与我试图在嵌入式 cmets 中解释的一样,并且可以从产生的输出中看出。这是 GNU 编译器中的错误吗?
诊断表明typedef int A; 范围内的行
struct B
错误:从“typedef double A”更改“A”的含义
请注意,当层次结构不是由template(当然Z c{36}; 声明被删除)时,clang 在C 范围内执行的查找(正确地,我想)会找到typedef 在 B 的范围内,并认为成员 a 属于
输入int;然后它会发出关于初始化double 常量2.2 缩小的警告...
【问题讨论】:
-
你能适当地格式化和缩短代码吗?
-
奇怪的缩进是怎么回事?
标签: c++ c++11 typedef language-lawyer name-lookup