为什么此模板代码在VS2010中有效,但在VS2012中无效?

时间:2012-10-01 13:08:00

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

我继承了一个大量使用模板元编程的项目,现在正处于从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年编译。此代码有什么问题?

1 个答案:

答案 0 :(得分:12)

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

相关问题