c++ - 针对 std::function 的 lambda 表达式和模板推导:为什么这有效?

标签 c++ lambda std-function template-argument-deduction

我的问题与这个问题有些相关: Lambdas and std::function 。请阅读此问题及其接受的答案。

因此,接受的答案表明以下代码使模板参数推导 lambda 成功,但为什么呢?

template<class T>
struct Identity{
  typedef T type;//why this helps?
};

template<typename BaseT>
vector<BaseT> findMatches(vector<BaseT> search, 
    typename Identity<function<bool (const BaseT &)>>::type func)
{
    vector<BaseT> tmp;

    for(auto item : search)
    {
        if( func(item) )
        {
            tmp.push_back(item);
        }
    }

    return tmp;
}

void Lambdas()
{
    vector<int> testv = { 1, 2, 3, 4, 5, 6, 7 };

    auto result = findMatches(testv, [] (const int &x) { return x % 2 == 0; });//lambda here.

    for(auto i : result)
    {
        cout << i << endl;
    }
}

int main(int argc, char* argv[])
{

    Lambdas();

    return EXIT_SUCCESS;
}

上面的代码可以工作,但我不知道为什么。首先我知道BaseT的模板推导lambda 不会发生这种情况,因为这是一个不可推导的上下文( Identify<function<bool (const BaseT &)>>::type )。相反,BaseTtestv 推导出来如int ,并且模板参数替换发生在 Identity<function<bool (const BaseT &)>>::type 中。

但是接下来会发生什么呢?替换后,Identity<function<bool (const BaseT &)>>::type变成function<bool (const BaseT &)> ,但是lambda表达式不是不是这种类型,并且模板参数推导中不会发生转换(尽管这里是模板参数替换)?

谢谢您的解释! 附:我似乎知道编译器为每个 lambda 表达式生成唯一命名的类。那么为什么lambda可以匹配function<bool (const BaseT &)>

最佳答案

But what's next? after substitution, Identity<function<bool (const BaseT &)>>::type becomes function<bool (const BaseT &)>, but isn't lambda expression not of that type, and conversions does not happen in template parameter deduction(although here is template parameter substitution)?

您是正确的,在模板参数推导过程中没有替换,但一旦完成,我们将函数调用视为调用实例化函数。 这意味着Identity<function<bool (const BaseT &)>>::type已替换为 function<bool (const BaseT &)>您正在调用的实例化函数是:

vector<int> findMatches(vector<int> search, function<bool (const int&)> func)

既然是混凝土function<bool (const int&)> lambda 可用于构造 func

关于c++ - 针对 std::function 的 lambda 表达式和模板推导:为什么这有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787005/

相关文章:

c++ - 一个 vector/映射中的 std::function 变量参数

c++ - C++ 项目中的外部库依赖项

c++ - 如何处理 "signed/unsigned mismatch"警告 (C4018)?

java - 我如何在 Java 8 lambda 中制作这段代码

C++ 在初始化 std::function 时,我们如何将占位符绑定(bind)到引用/引用参数?

c++ - 具有默认 std::function 参数的模板函数导致符号 [...] 已经定义

c++ - OpenGL 在正射和透视问题之间切换

c++ - lambda 函数中的 Const 引用参数

python - python中的常量lambda函数

python - python 中是否有与 Linq.Expressions.Expression 等效的东西?