简体   繁体   English

错误:不匹配的类型:<function> 和<lambda></lambda></function>

[英]error: mismatched types: <function> and <lambda>

I wrote a simple function which gives me an index when using std::for_each over an iterator.我写了一个简单的 function 在迭代器上使用std::for_each时给了我一个索引。 The function is as follows: function如下:

template<typename It, typename I = std::size_t>
void for_each_indexed(It begin,
                      It end,
                      void l(typename std::iterator_traits<It>::value_type, I),
                      I counter = 0) {
    std::for_each(begin, end,
        [&counter, &l](typename std::iterator_traits<It>::value_type value) {
            l(value, counter);
            counter++;
        });
};

My problem is that, when I pass a lambda to this function, I get an error like the following:我的问题是,当我将 lambda 传递给此 function 时,出现如下错误:

test.cpp:40:6: error: no matching function for call to ‘for_each_indexed(std::__cxx11::
basic_string<char>::iterator, std::__cxx11::basic_string<char>::iterator, crossOutChar(
std::__cxx11::string&, char)::<lambda(char, size_t)>)’
     });
      ^
test.cpp:18:6: note: candidate: ‘template<class It, class I> void for_each_indexed(It, 
It, void (*)(typename std::iterator_traits<_Iter>::value_type, I), I)’
 void for_each_indexed(It begin,
      ^~~~~~~~~~~~~~~~
test.cpp:18:6: note:   template argument deduction/substitution failed:
test.cpp:40:6: note:   mismatched types ‘void (*)(typename std::iterator_traits<_Iter>:
:value_type, I)’ and ‘crossOutChar(std::__cxx11::string&, char)::<lambda(char, size_t)’
     });
      ^

The following compiles and runs:以下编译并运行:

void process(char c, size_t i) {}

void crossOutChar(std::string &s, char c) {
    for_each_indexed(s.begin(), s.end(), process);
}

but the following does not:但以下没有:

void
crossOutChar(std::string &s, char c) {
    auto process = [c, &s](char c2, size_t i) {
      if (c == c2) {
        s[i] = '*';
      }
    };

    for_each_indexed(s.begin(), s.end(), process);
}

What could be the error in here?这里可能是什么错误? Thanks in advance.提前致谢。

Captureful lambdas cannot be cast to pointers-to-functions.捕获的 lambda 不能转换为指向函数的指针。 You may change your function proto to something like您可以将您的 function 原型更改为类似

template<class F, typename It, typename I = std::size_t>
void for_each_indexed(It begin,
                      It end,
                      F &&l,
                      I counter = 0)

As a side note, integral objects ( counter here) are better captured by value.附带说明一下,整数对象(此处为counter )更好地按值捕获。

First: you can't cast your lambda expression to function pointer, because it captures.首先:您不能将 lambda 表达式转换为 function 指针,因为它会捕获。

To work with both cases you cited, you have to use std::function .要处理您引用的两种情况,您必须使用std::function But, due to the cast to std::function , the compiler will have problems to deduce I even if you set a default argument.但是,由于转换为std::function ,即使您设置了默认参数,编译器也将无法推断I Then, you have some options:然后,您有一些选择:

Explicitly set std::size_t :显式设置std::size_t

std::function<void(typename std::iterator_traits<It>::value_type, std::size_t)> l

Or create another template argument for function:或者为 function 创建另一个模板参数:

template<typename It, typename F, typename I = std::size_t>
void for_each_indexed(It begin,
                      It end,
                      F&& l,
                      I counter = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM