c++ - Lambda 作为模板参数

标签 c++ templates c++11 lambda

我发现了那些类似的问题Lambda expressions as class template parametersHow to use a lambda expression as a template parameter? ,但即使有了可用的答案,我还是不明白为什么以下代码不起作用(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 的第二个参数是一个“比较器”,在本例中是一个 lambda,在我的代码中,我正在尝试做同样的事情,或者至少我认为我正在做同样的事情,但我的代码不起作用。我的代码中缺少什么?

另请注意来自 Xeo 的 Lambda expressions as class template parameters

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;
}

关于c++ - Lambda 作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28616943/

相关文章:

wpf - 免费的WPF应用程序模板?

python - 如何使用 format() 转义 nginx 关键字

c++ - 使用 OpenCV/C++ 进行运动检测,阈值始终为零

c++ - 从(成员)函数推导出类型

c++ - 如何监控放入标准输出缓冲区的内容并在特定字符串存放在管道中时中断?

c++ - 带有类getter函数的奇怪输出C++

c++ - 使用可变参数模板(gcc、clang)的成员函数指针包装器

c++ - std::reference_wrapper 对比整数&

c++ - 嵌套 vector 的缺点是什么?

C++11:模板中的(递归)指针算术 (TMP)