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

查看:209
本文介绍了具有非静态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
看起来像一个错误。我提交了错误报告

推荐答案

我认为这是一个g ++ bug与默认成员初始化。我对此并不积极,因此有以下支持证据:

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天全站免登陆