具有非静态lambda成员的类不能使用默认模板参数吗? [英] Class with non-static lambda member can't use default template paramers?

查看:51
本文介绍了具有非静态lambda成员的类不能使用默认模板参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个小型测试程序:

#include <functional>

//template<class T>            // <-- with this, gcc compiles  ok
template<class T=void>
struct  c{
        std::function<int(int)> f = [](int i){return i+i;};
};

int main() {};

Clang-3.2可以正常编译,但是从GCC 4.7.1和4.8中我得到了奇怪的错误:

Clang-3.2 compiles it ok, but from GCC 4.7.1 and 4.8 I am getting strange error:

t.cc:6:31: error: default argument for template parameter for class enclosing ‘struct __lambda0’
  function<int(int)> f = [](int i){return i+i;};
                               ^

这是没人知道的那些晦涩的C ++规则异常之一,还是GCC错误?

Is this one of those obscure C++ rules exceptions that nobody knows about or is it a GCC bug?

编辑看起来像个虫子.我已经提交了错误报告

EDIT Looks like a bug. I've filed bug report

推荐答案

我认为这是带有默认成员初始化的g ++错误.我对此并不满意,因此有以下支持证据:

I think this is a g++ bug with default member initialization. I'm not positive about this, and so have the following supporting evidence:

template<class T=void>
struct  c {
   std::function<int(int)> f;
   c() : f([](int i){return i+i;}) {
   }
};

int main() {}

如果这行得通,那么您所做的也应该行得通.即使您构造了 c .

If that works, what you're doing should work too. And it does, even if you construct a c.

就个人而言,我认为应谨慎使用默认成员初始化.我认为这很容易造成混乱,因为大多数人希望所有初始化都在构造函数中完成,而成员初始化器不一定在任何构造函数附近.这样一来,他们可能会让人不知所措,想知道某个成员如何获得特定的价值.

Personally, I think default member initialization should be used sparingly and with care. I think it's really easy to create a lot of confusion with it because most people expect all the initialization to be done in the constructor, and member initializers are not necessarily anywhere near any constructor. So they can leave someone scratching their head wondering how some member gets a particular value.

我可以看到一些情况,尤其是对于简单的,主要是数据类的情况,这种情况下效果很好.但是大多数情况下,我认为如果您拥有任何类型的构造函数体,则可能不应该使用默认的成员初始化.

I can see cases, especially with simple, mostly-data classes for which it would work pretty well. But mostly, I think if you have a constructor body of any kind, you should probably not be using default member initialization.

这篇关于具有非静态lambda成员的类不能使用默认模板参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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