【发布时间】:2011-09-08 02:31:47
【问题描述】:
This snippet(取自this question)可以用g++编译(如图所示),只要template在返回类型之前存在。相比之下,VC10 不编译该代码并出现以下错误:
错误 C2244: 'A::getAttr' : 无法将函数定义与现有声明匹配
如果我删除template,VC10 很高兴,但 g++ 会发出此错误:
错误:非模板“AttributeType”用作模板
注意:使用 'A::template AttributeType' 表示它是一个模板
又是VC破了两相查找还是什么原因?哪个编译器在这里?我怀疑 g++ 是正确的,因为我对这里需要 template 有一个模糊的记忆,就像分配器中的 rebind 模板一样。
编辑:我们有一个赢家:g++/GCC(惊喜...)。
template <typename T, typename K>
class A {
public:
T t;
K k;
template <int i, int unused = 0>
struct AttributeType{
};
template <int i>
AttributeType<i> getAttr();
};
template <typename T, typename K>
template <int i>
typename A<T, K>::template AttributeType<i> A<T, K>::getAttr() {
// ^^^^^^^^ -- needed or not?
return t;
}
int main(){
A<int,int> a;
}
【问题讨论】:
-
发布问题中的代码。堆栈溢出支持并强烈鼓励这样做。
-
一般来说,用于标准合规性的非正式“参考编译器”是 Comeau,您可以尝试提交您的 sn-p here 以查看其判断。 - 编辑:现在试过了,sn-p 编译得很好,就像在 Ideone 上一样。
-
clang++ 2.9 和 Intel C/C++ 11.1 端也带有 gcc。
-
等等,你是在问 MSVC 或 gcc 是否能正确使用
template和typename关键字? MSVC 10?严重地? MS 对templates 的实现在当前版本中仍然非常糟糕,更不用说那么旧的了。 -
@Yakk 你确实意识到这个问题已经超过 3 年了?
标签: c++ templates visual-c++ g++ correctness