【问题标题】:Why does this template code work in VS2010, but not in VS2012?为什么此模板代码在 VS2010 中有效,而在 VS2012 中无效?
【发布时间】: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&lt;i&gt;,只是typedef不行。

这个例子是在 2010 年编译的,但不是在 2012 年编译的。这段代码有什么问题?

【问题讨论】:

  • 不是 100%(因此不是答案),但我相信您缺少 template: typedef typename T::template Nested&lt;i&gt; Found; 或类似的东西。
  • 只是添加到 David 的信息中,将 Visual C++ 模板代码公开给 g++ 总是一个好主意。通常,要使代码至少使用两个不同的编译器进行编译。在过去,认真对待它的人们使用 Comeau,但我不确定它是否已更新为更完整的 C++11 一致性。

标签: c++ templates template-meta-programming visual-c++-2012


【解决方案1】:

每个VS版本对templatetypename的要求越来越严格。你错过了template,VS2012 抱怨是对的。

【讨论】:

  • 你可能会指出在哪里。 (在Nested 之前,因为在模板实例化之前编译器无法猜测T::Nested 是模板)
猜你喜欢
  • 2011-09-09
  • 2022-10-07
  • 2021-07-05
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2011-05-25
相关资源
最近更新 更多