模板外的类名 [英] typename outside of template

查看:332
本文介绍了模板外的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在VS2010sp1不编译(它确实编译gcc 4.6虽然):

This in VS2010sp1 doesn't compile (it does compile with gcc 4.6 though):

template<class T>
struct Upcast;

template<>
struct Upcast<signed char>
{
    typedef signed short type;
};

template<>
struct Upcast<char>
{
    typedef typename std::conditional<std::is_signed<char>::value,short, unsigned short>::type type;
};

int main()
{
    Upcast<char>::type a;
    return 0;
}

VS发生错误:

Error   1   error C2899: typename cannot be used outside a template declaration


$ b b

哪个球队是对的? VS或gcc?

Which team is right? VS or gcc?

推荐答案

VS是正确的C ++ 03。 GCC是正确的C ++ 0x。

VS is right on C++03. GCC is right on C++0x.

现在可能是明智的GCC也允许这在C + + 03模式(有很多事情真正的编译器不诊断在C + + 03模式,实际上只在C ++ 0x中有效),并且VS可以在C ++ 03模式中拒绝它。

Now it may be sensible for GCC to also allow this in C++03 mode (there are many things real compilers don't diagnose in C++03 mode that are actually only valid in C++0x), and it may as-well sensible for VS to reject it in C++03 mode.

没关系无论是否在模板中使用 typename QualifiedName ,在C ++ 0x中。也就是说,以下是C ++ 0x完全合法的:

It doesn't matter anymore whether or not a use of typename QualifiedName happens in a template or not, in C++0x. That is, the following is perfectly legal for C++0x:

#include<vector>

int main() {
  typename std::vector<int> v;
}

在C ++ 03中, typename 只能在模板中使用。和你的代码中的显式专门化不是一个模板。没有模板< typename T ...> 子句(代码中的所有参数都是固定的)。

In C++03, typename could only be used inside of a template. And the explicit specialization in your code is not a template. There are no template<typename T ...> clauses (all parameters in your code are fixed).

这篇关于模板外的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆