【发布时间】:2018-11-25 11:17:05
【问题描述】:
为什么赋值运算符不允许在声明对象的同一行中使用 lambda 表达式?
它似乎在 MSVC 中工作。
测试代码: https://godbolt.org/g/n2Tih1
class Func
{
typedef void(*func_type)();
func_type m_f;
public:
Func() {}
Func(func_type f) : m_f(f) {}
Func operator=(func_type f) {
m_f = f;
return *this;
}
};
int main()
{
// doesn't compile in GCC and clang, it does in MSVC
Func f1 = []() {
};
// compiles!
Func f2;
f2 = []() {
};
// compiles!
Func f3([]() {
});
}
【问题讨论】:
-
试试
Func f4 = +[](){};:-) -
@Jarod42 太棒了!为什么? o.0
-
它类似于 François Andrieux 的回答中的
static_cast<void(*)()>([]() {})。
标签: c++ c++11 lambda initialization variable-assignment