简体   繁体   中英

error: mismatched types: <function> and <lambda>

I wrote a simple function which gives me an index when using std::for_each over an iterator. The function is as follows:

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:

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. You may change your function proto to something like

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.

First: you can't cast your lambda expression to function pointer, because it captures.

To work with both cases you cited, you have to use 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. Then, you have some options:

Explicitly set std::size_t :

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

Or create another template argument for function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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