本地类中的成员模板 [英] Member template in local class

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

问题描述

给定以下代码:

 void f()
 {
     class A
     {
         template <typename T>
         void g() {}
     };
 }

g ++ 4.4(以及 g ++ - 4.6 -std = gnu ++ 0x )抱怨:本地类中成员模板的无效声明。

g++ 4.4 (and also g++-4.6 -std=gnu++0x) complains: "invalid declaration of member template in local class".

显然本地类不允许有模板成员。这个限制的目的是什么?将在C ++ 0x中删除?

Apparently local classes are not allowed to have template members. What is the purpose of this limitation? Will it be removed in C++0x?

注意:如果我让本地类本身是一个模板,而不是给它一个模板成员:

Note: If I make the local class itself a template, rather than giving it a template member:

 void f()
 {
     template <typename T>         
     class A
     {
         void g() {}
     };
 }

我得到错误:模板声明不能出现在块范围。

I get "error: a template declaration cannot appear at block scope".

推荐答案

此限制的目的是什么?只是猜测,但:

The purpose of this limitation? Just a guess, but:


  • 您可以仅在封闭函数中使用模板类/模板成员函数。因此,您已经知道函数中所有使用的类型,因此可以直接指定使用的类型(对于多种类型,当然,模板变体会保存一些类型)。

  • 看起来不是这样,它是所有编译器创建者和错误的空间的工作,所以它一定是值得的努力。

尝试在函数中使用局部类作为函数中声明的(c ++ 0x)-lambda函数的返回类型:MSVC 2010:内部编译器错误^^。

Fun Fact: Try to use a local class within a function as a return type for a (c++0x)-lambda function declared in the function: MSVC 2010: internal compiler error ^^.

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

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