首页 > 解决方案 > C++ 在初始化 std::function 时,我们如何将占位符绑定到引用/引用参数?

问题描述

#include <functional>
#include <string>

int fn( int a, int & b ) { return a+b; }

struct Fn_struct {
    std::string name {};
    // std::function<int (int,int&)> my_fn {};
    std::function<decltype(fn)> my_fn {};
};

int main()
{
    Fn_struct my_fn_struct1 {"fn(a,b)", std::function<decltype (fn)> {fn} };
    Fn_struct my_fn_struct2 {"fn(a,b)", {fn} };
    Fn_struct my_fn_struct3 {"fn(a,b)", {std::bind( fn, 1, 2) }};
    auto fn_b = std::bind( fn, 1, std::placeholders::_1 );
    Fn_struct my_fn_struct4 {"fn(a,b)", {std::bind( fn, 1, std::placeholders::_1) }};  // todo: why can't int b be a reference?
}

my_fn_struct4 无法编译,因为找不到绑定的构造函数。但是,如果 b 不是参考,它会编译。

另一方面 fn_b 确实编译。

任何解释将不胜感激。

请不要问我为什么要这样做。除非完全必要,否则我宁愿不使用指针来完成此任务。

标签: c++std-functionstdbind

解决方案


std::bind( fn, 1, std::placeholders::_1 )返回一个可转换为的对象,std::function<int(int &)> my_fn{};因为传递了一个带有 2 个参数的函数并且第一个参数绑定为 1:

#include <functional>
#include <string>

int fn( int a, int & b ) { return a+b; }

struct Fn_struct {
    std::string name {};
    std::function<int(int &)> my_fn{};
};

int main()
{
    Fn_struct my_fn_struct4 {"fn(a,b)", {std::bind( fn, 1, std::placeholders::_1) }};
}

线

Fn_struct my_fn_struct3 {"fn(a,b)", {std::bind( fn, 1, 2) }};

工作,因为

如果在调用 g() 时提供的某些参数与存储在 g 中的任何占位符都不匹配,则评估并丢弃未使用的参数。

https://en.cppreference.com/w/cpp/utility/functional/bind


推荐阅读