Lambda作为模板参数
Lambda as template parameter
我发现了类似的问题Lambda表达式作为类模板参数和如何使用Lambda表达式作为模板参数?,但即使有了答案,我也不明白为什么以下代码不起作用(g++4.8.2和g++-4.9):
auto GoLess = [](int a,int b) -> bool
{
return a < b;
};
template<typename Order>
struct foo
{
int val;
bool operator<(const foo& other)
{
return Order(val, other.val);
}
};
typedef foo<decltype(GoLess)> foo_t;
int main()
{
foo_t a,b;
bool r = a < b;
}
编译器输出为:
test.cpp: In instantiation of ‘bool foo<Order>::operator<(const foo<Order>&) [with Order = <lambda(int, int)>]’:
test.cpp:26:15: required from here
test.cpp:17:30: error: no matching function for call to ‘<lambda(int, int)>::__lambda0(int&, const int&)’
return Order(val, other.val);
^
test.cpp:17:30: note: candidates are:
test.cpp:5:16: note: constexpr<lambda(int, int)>::<lambda>(const<lambda(int, int)>&)
auto GoLess = [](int a,int b) -> bool
^
test.cpp:5:16: note: candidate expects 1 argument, 2 provided
test.cpp:5:16: note: constexpr<lambda(int, int)>::<lambda>(<lambda(int, int)>&&)
test.cpp:5:16: note: candidate expects 1 argument, 2 provided
这个代码不应该工作吗?根据我的理解,从其他线程中阅读这些代码应该编译,但不是。
非常感谢
附录:
为了澄清一点,在上述问题中,KennyTM编写了以下代码:
auto comp = [](const A& lhs, const A& rhs) -> bool { return lhs.x < rhs.x; };
auto SetOfA = std::set <A, decltype(comp)> (comp);
这应该有效,std::set的第二个参数是一个"comparator",在本例中是一个lambda,在我的代码中,我试图做同样的事情,或者至少我认为我在做同样的事,但我的代码不起作用。我的代码中缺少什么?
还请注意,在Lambda表达式中的Xeo作为类模板参数
auto my_comp = [](const std::string& left, const std::string& right) -> bool {
// whatever
}
typedef std::unordered_map<
std::string,
std::string,
std::hash<std::string>,
decltype(my_comp)
> map_type;
同样,这应该有效。我的错在哪里?
感谢
正如编译器告诉您的那样,问题出在这一行:
return Order(val, other.val);
由于Order
是一个类型(不是函数),因此它正在调用Order的双参数构造函数。但它没有。
调用函数类的正确语法是:
return Order()(val, other.val);
然而,这也不起作用,因为为lambda生成的类有一个已删除的默认构造函数。
简而言之,您需要使用lambda的(唯一)实例来实例化您的类。
以下是一种可能的方法:
template<typename Order>
struct foo {
foo(Order compare) : compare_(compare) {}
bool operator<(const foo& other) {
return compare_(val, other.val);
}
int val;
Order compare_;
};
/* In practice, you'll want to template a parameter pack
* for the other arguments to the constructor, since those are likely.
* Also, you might want to use std::forward.
*/
template<typename Order>
foo<Order> foomaker(Order order) {
return foo<Order>(order);
}
int main() {
auto GoLess = [](int a,int b) -> bool
{
return a < b;
};
auto a = foomaker(GoLess);
auto b = foomaker(GoLess);
bool r = a < b;
return r;
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何将lambda作为模板类的成员函数参数
- 使用自动推导的 lambda 参数作为常量表达式
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 引用捕获和在 lambda 中通过引用发送参数有什么区别 (C++)
- 将__device__ lambda 作为参数传递给 __global__ 函数
- 将 lambda 函数作为参数传递C++
- 如何将 lambda 函数作为参数发送到另一个函数
- Lambda可以用作非类型模板参数吗
- 将参数传递给泛型 lambda 时复制构造函数不正确
- 如何确定捕获不可复制参数的 lambda 的类型?
- 省略C++可变参数 lambda 中的"auto"关键字?
- 如何访问可变参数 lambda 函数参数
- 可变参数 lambda 捕获的解决方法
- 传递给多个参数 lambda 表达式的参数
- 如何编写丢弃其参数的通用可变参数 lambda
- 使用c++参数lambda函数
- 将函数模板"pass"为泛型可变参数 lambda 返回语句是好方法吗?
- c++模板非类型参数lambda函数