[英]Why can't I use a lambda as a comparator for a set defined in a class?
This is fairly standard and works fine:这是相当标准的并且工作正常:
#include <set>
auto cmp = [](int a, int b) { return a > b; };
using stype = std::set<int, decltype(cmp)>;
stype mySet(cmp);
But why can't I do this?但是为什么我不能这样做呢?
#include <set>
auto cmp = [](int a, int b) { return a > b; };
using stype = std::set<int, decltype(cmp)>;
struct foo {
stype mySet(cmp);
};
This fails with Unknown type name 'cmp'
(in XCode 5.1.1, compiling for Mac using llvm, in case it's relevant.)这因
Unknown type name 'cmp'
失败(在 XCode 5.1.1 中,使用 llvm 为 Mac 编译,以防相关。)
What is the reason for this error, and how can I fix / work around it?此错误的原因是什么,我该如何修复/解决它? Do I have to avoid using a lambda for this purpose, and if so, why?
我是否必须避免为此目的使用 lambda,如果是,为什么?
My actual code has several classes that store objects of type stype::iterator
to point to items inside foo::mySet
, so I do want to declare stype
outside the struct.我的实际代码有几个类存储
stype::iterator
类型的对象以指向foo::mySet
内的项目,所以我想在结构外声明stype
。
A non-static data member initializer must be a brace-or-equal-initializer .非静态数据成员初始值设定项必须是大括号或相等初始值设定项。 The following should work:
以下应该工作:
stype mySet {cmp};
stype mySet = stype(cmp);
However, stype mySet(cmp);
但是,
stype mySet(cmp);
is parsed as declaring a member function named mySet
that returns stype
and takes an unnamed argument of type cmp
, hence the error.被解析为声明名为
mySet
的成员函数,该成员函数返回stype
并采用类型为cmp
的未命名参数,因此出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.