Home >Backend Development >C++ >Why Does Type Deduction Fail with std::function and Lambda Functions in C 11?

Why Does Type Deduction Fail with std::function and Lambda Functions in C 11?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-20 05:50:10325browse

Why Does Type Deduction Fail with std::function and Lambda Functions in C  11?

Type Deduction with std::function and Lambda Functions in C 11

When working with templates in C 11, type deduction can be a powerful feature. However, there are certain scenarios where type deduction fails, specifically when std::function or lambda functions are involved.

In the following example, the function test accepts a set of type A and returns the same set without explicitly specifying the template type:

template<class A>
set<A> test(const set<A>&amp; input) {
    return input;
}

Calling this function with test(mySet) elsewhere in the code works as expected. However, when using a function with a lambda function, type deduction fails:

template<class A>
set<A> filter(const set<A>&amp; input,function<bool(A)> compare) {
    // ... implementation
}

Trying to call filter with filter(mySet,[](int i) { return i%2==0; }); results in the error:

error: no matching function for call to ‘filter(std::set&amp;, main()::)’

This issue arises because lambdas are not functions or std::function objects. They are function objects with specific properties defined by the standard. Type deduction only works with exact types, and lambdas do not meet this criterion.

To resolve this, you can convert the lambda to a std::function or provide the template type explicitly:

// Convert lambda to std::function
std::function<bool(int)> func = [](int i) { return i%2 ==0; };
set<int> myNewSet = filter(mySet,func);

// Provide template type explicitly
set<int> myNewSet = filter<int>(mySet,[](int i) { return i%2==0; });

Alternatively, you can redefine the function to accept a template parameter for the CompareFunction:

template<class A,class CompareFunction>
set<A> filter(const set<A>&amp; input,CompareFunction compare) {
    // ... implementation
}

With this modification, you can call the function without specifying the template type:

set<int> result = filter(myIntSet,[](int i) { i % 2 == 0; });

Type deduction can also be problematic with multi-argument lambdas. In such cases, using auto can be a useful solution:

template<class Value,class CompareType,class IndexType>
auto filter(const set<Value>&amp; input,CompareType compare,IndexType index) -> map<decltype(index(*(input.begin()))),Value> {
    // ... implementation
}

This function can be called as follows:

map<string,int> s = filter(myIntSet,[](int i) { return i%2==0; },[](int i) { return toString(i); });

The above is the detailed content of Why Does Type Deduction Fail with std::function and Lambda Functions in C 11?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn