1. <small id='C2Vjc'></small><noframes id='C2Vjc'>

      <legend id='C2Vjc'><style id='C2Vjc'><dir id='C2Vjc'><q id='C2Vjc'></q></dir></style></legend>

      <tfoot id='C2Vjc'></tfoot>
    2. <i id='C2Vjc'><tr id='C2Vjc'><dt id='C2Vjc'><q id='C2Vjc'><span id='C2Vjc'><b id='C2Vjc'><form id='C2Vjc'><ins id='C2Vjc'></ins><ul id='C2Vjc'></ul><sub id='C2Vjc'></sub></form><legend id='C2Vjc'></legend><bdo id='C2Vjc'><pre id='C2Vjc'><center id='C2Vjc'></center></pre></bdo></b><th id='C2Vjc'></th></span></q></dt></tr></i><div id='C2Vjc'><tfoot id='C2Vjc'></tfoot><dl id='C2Vjc'><fieldset id='C2Vjc'></fieldset></dl></div>

      • <bdo id='C2Vjc'></bdo><ul id='C2Vjc'></ul>

      将 lambda 传递给函数模板

      时间:2023-05-25
        <legend id='vc9gr'><style id='vc9gr'><dir id='vc9gr'><q id='vc9gr'></q></dir></style></legend>
          <bdo id='vc9gr'></bdo><ul id='vc9gr'></ul>
          <i id='vc9gr'><tr id='vc9gr'><dt id='vc9gr'><q id='vc9gr'><span id='vc9gr'><b id='vc9gr'><form id='vc9gr'><ins id='vc9gr'></ins><ul id='vc9gr'></ul><sub id='vc9gr'></sub></form><legend id='vc9gr'></legend><bdo id='vc9gr'><pre id='vc9gr'><center id='vc9gr'></center></pre></bdo></b><th id='vc9gr'></th></span></q></dt></tr></i><div id='vc9gr'><tfoot id='vc9gr'></tfoot><dl id='vc9gr'><fieldset id='vc9gr'></fieldset></dl></div>

              <tbody id='vc9gr'></tbody>
            <tfoot id='vc9gr'></tfoot>

                <small id='vc9gr'></small><noframes id='vc9gr'>

                本文介绍了将 lambda 传递给函数模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在学习 C++,我正在尝试实现一个二分查找函数,该函数查找谓词所包含的第一个元素.该函数的第一个参数是一个向量,第二个参数是一个计算给定元素谓词的函数.二分查找函数如下所示:

                I'm learning C++, and I'm trying to implement a binary search function that finds the first element for which a predicate holds. The function's first argument is a vector and the second argument is a function that evaluates the predicate for a given element. The binary search function looks like this:

                template <typename T> int binsearch(const std::vector<T> &ts, bool (*predicate)(T)) {
                    ...
                }
                

                如果这样使用,它会按预期工作:

                This works as expected if used like this:

                bool gte(int x) {
                    return x >= 5;
                }
                
                int main(int argc, char** argv) {
                    std::vector<int> a = {1, 2, 3};
                    binsearch(a, gte);
                    return 0;
                }
                

                但是如果我使用 lambda 函数作为谓词,我会得到一个编译器错误:

                But if I use a lambda function as a predicate, I get a compiler error:

                search-for-a-range.cpp:20:5: error: no matching function for call to 'binsearch'
                    binsearch(a, [](int e) -> bool { return e >= 5; });
                    ^~~~~~~~~
                search-for-a-range.cpp:6:27: note: candidate template ignored: could not match 'bool (*)(T)' against '(lambda at
                      search-for-a-range.cpp:20:18)'
                template <typename T> int binsearch(const std::vector<T> &ts,
                                          ^
                1 error generated.
                

                以上错误是由

                binsearch(a, [](int e) -> bool { return e >= 5; });
                

                怎么了?为什么编译器不相信我的 lambda 有正确的类型?

                What's wrong? Why is the compiler not convinced that my lambda has the right type?

                推荐答案

                你的函数 binsearch 接受一个函数指针作为参数.lambda 和函数指针是不同的类型:可以将 lambda 视为实现 operator() 的结构体实例.

                Your function binsearch takes a function pointer as argument. A lambda and a function pointer are different types: a lambda may be considered as an instance of a struct implementing operator().

                请注意,无状态 lambda(不捕获任何变量的 lambda)可隐式转换为函数指针.由于模板替换,这里隐式转换不起作用:

                Note that stateless lambdas (lambdas that don't capture any variable) are implicitly convertible to function pointer. Here the implicit conversion doesn't work because of template substitution:

                #include <iostream>
                
                template <typename T>
                void call_predicate(const T& v, void (*predicate)(T)) {
                    std::cout << "template" << std::endl;
                    predicate(v);
                }
                
                void call_predicate(const int& v, void (*predicate)(int)) {
                    std::cout << "overload" << std::endl;
                    predicate(v);
                }
                
                void foo(double v) {
                    std::cout << v << std::endl;
                }
                
                int main() {
                    // compiles and calls template function
                    call_predicate(42.0, foo);
                
                    // compiles and calls overload with implicit conversion
                    call_predicate(42, [](int v){std::cout << v << std::endl;});
                
                    // doesn't compile because template substitution fails
                    //call_predicate(42.0, [](double v){std::cout << v << std::endl;});
                
                    // compiles and calls template function through explicit instantiation
                    call_predicate<double>(42.0, [](double v){std::cout << v << std::endl;});
                }
                

                <小时>

                你应该让你的函数 binsearch 更通用,比如:

                template <typename T, typename Predicate>
                T binsearch(const std::vector<T> &ts, Predicate p) {
                
                    // usage
                
                    for(auto& t : ts)
                    {
                        if(p(t)) return t;
                    }
                
                    // default value if p always returned false
                
                    return T{};
                }
                

                从标准算法库中汲取灵感.

                这篇关于将 lambda 传递给函数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:链接器如何处理跨翻译单元的相同模板实例化? 下一篇:reinterpret_cast 会导致未定义的行为吗?

                相关文章

                最新文章

                • <bdo id='1MyB5'></bdo><ul id='1MyB5'></ul>
              1. <tfoot id='1MyB5'></tfoot>

                  1. <i id='1MyB5'><tr id='1MyB5'><dt id='1MyB5'><q id='1MyB5'><span id='1MyB5'><b id='1MyB5'><form id='1MyB5'><ins id='1MyB5'></ins><ul id='1MyB5'></ul><sub id='1MyB5'></sub></form><legend id='1MyB5'></legend><bdo id='1MyB5'><pre id='1MyB5'><center id='1MyB5'></center></pre></bdo></b><th id='1MyB5'></th></span></q></dt></tr></i><div id='1MyB5'><tfoot id='1MyB5'></tfoot><dl id='1MyB5'><fieldset id='1MyB5'></fieldset></dl></div>

                    <small id='1MyB5'></small><noframes id='1MyB5'>

                    <legend id='1MyB5'><style id='1MyB5'><dir id='1MyB5'><q id='1MyB5'></q></dir></style></legend>