本地类型作为 C++ 中的模板参数 [英] Local type as template arguments in C++

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

问题描述

这是我的代码

#include <向量>模板<类型名T,模板<类型名>C 类 = std::vector >结构FooBar{/*编码*/};模板结构全局{};int main(){结构本地{};FooBar<本地,全球>k;}

这是我得到的错误

‘template 的模板参数C级>struct FooBar' 使用本地类型'main()::Local'

标准的哪一部分说这是错误的?我正在使用 gcc 4.5.1.如何使此代码工作?

解决方案

标准的哪一部分说这是错误的?

这将是 2003 C++ 标准中的 §14.3.1/2:

<块引用>

局部类型、没有链接的类型、未命名的类型或由这些类型中的任何一种复合的类型不得用作模板类型参数的模板参数.

<小时><块引用>

如何使此代码工作?

不要使用本地类型作为模板参数.

请注意,此限制已在 C++11 中取消,因此使用该语言标准,您可以使用本地类型作为模板参数.

This is my code

#include <vector>
template <typename T, template<typename> class C = std::vector >
struct FooBar
{
   /*codez*/
};
template<typename T>
struct Global{};

int main()
{
   struct Local{};  
   FooBar<Local,Global> k;
}

This is the error that I get

template argument for ‘template<class T, template<class> class C> struct FooBar’ uses local type ‘main()::Local’

Which part of the standard says that this is wrong? I am using gcc 4.5.1. How can make this code work?

解决方案

Which part of the standard says that this is wrong?

That would be §14.3.1/2 from the 2003 C++ Standard:

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.


How can make this code work?

Don't use a local type as a template argument.

Note that this restriction has been lifted in C++11, so using that language standard you are able to use a local type as a template argument.

这篇关于本地类型作为 C++ 中的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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