模板类的方法使用全局范围内的 typedef.编译器错误
Method of a template class uses a typedef from the global scope. Compiler bug?
我在VC2010中有一个代码,我把它简化为一个小例子。
Test.h:
#pragma once
template <typename TPixel>
struct Image
{
typedef TPixel PixelType;
};
template <typename TImageType>
struct Operation
{
void DoOperation()
{
ImageType::PixelType value = 0;
// I've done a misprint here. It should be TImageType::PixelType
}
};
Test.cpp:
void Test()
{
typedef Image<char> ImageType;
Operation<ImageType> op;
op.DoOperation();
}
正如我所料,这会产生一个错误。
test.h(14): error C2653: 'ImageType' : is not a class or namespace name
现在,让我们稍微改变一下test.cpp
。
typedef Image<char> ImageType;
void Test()
{
Operation<ImageType> op;
op.DoOperation();
}
现在它编译了!令人惊讶的是,ImageType
in DoOperation()
现在与 test.cpp
中的全局 typedef 匹配。
我的问题:为什么要编译?这是视觉C++错误还是标准行为?
我认为test.cpp
在typedef之前包含test.h
,所以它实际上是
#include "test.h"
typedef Image<char> ImageType;
void Test()
{
Operation<ImageType> op;
op.DoOperation();
}
像这样完成,它确实是一个错误,或者关于两阶段查找的标准不合格行为。不依赖于模板参数的名称应相对于模板的声明点进行解析。
我想这是已知的行为。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 未在此范围内声明错误 'xy'
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 命名空间范围内的外部 - GCC vs clang vs msvc
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 如何仅使用 While 循环在给定范围内找到可被 7 整除的计数整数
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- 查找给定范围内最长连续 1 的频率
- "myStateType"未在此范围内声明,原因 #typedef
- 模板类的方法使用全局范围内的 typedef.编译器错误
- 在本地范围内重新定义typedef