【发布时间】:2012-10-01 13:07:59
【问题描述】:
我继承了一个大量使用模板元编程的项目,现在正在从 Visual Studio 2010 升级到 2012。一些模板代码在 2012 年不再工作。我提炼了一个最小的例子:
template <typename T, int i>
class MyClass
{
private:
typedef typename T::Nested<i> Found;
};
给出这个错误信息:
source.cpp(5): error C2059: syntax error : '<'
source.cpp(6) : see reference to class template instantiation 'MyClass<T,i>' being compiled
source.cpp(5): error C2238: unexpected token(s) preceding ';'
再往下MyClass,我可以用T::Nested<i>,只是typedef不行。
这个例子是在 2010 年编译的,但不是在 2012 年编译的。这段代码有什么问题?
【问题讨论】:
-
不是 100%(因此不是答案),但我相信您缺少
template:typedef typename T::template Nested<i> Found;或类似的东西。 -
只是添加到 David 的信息中,将 Visual C++ 模板代码公开给 g++ 总是一个好主意。通常,要使代码至少使用两个不同的编译器进行编译。在过去,认真对待它的人们使用 Comeau,但我不确定它是否已更新为更完整的 C++11 一致性。
标签: c++ templates template-meta-programming visual-c++-2012