人鬼对镜妆 發表於 2025-12-11 09:20:38

C++中的bind实践代码

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1.std::bind是什么?</li><li>2. 核心机制:占位符 (std::placeholders)</li><li>3. 代码实战</li><ul class="second_class_ul"><li>场景 A:固定参数(减少参数个数)</li><li>场景 B:参数重排(改变参数顺序)</li><li>场景 C:绑定成员函数(最常用的旧式写法)</li></ul><li>4. 这里的坑:参数拷贝 vs 引用</li><ul class="second_class_ul"></ul><li>5. 灵魂拷问:为什么现在不推荐用std::bind?</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>1.std::bind是什么?</h2>
<p>简单来说,<code>std::bind</code> 是一个<strong>函数适配器(Adapter)</strong>。</p>
<p>核心作用是&ldquo;预先固定&rdquo;<strong>一个函数的某些参数,从而生成一个新的函数对象。</strong></p>
<p>你可以把它想象成一个<strong>模具</strong>:原来的函数需要 3 个原料,你先塞进去 1 个固定的原料,剩下的 2 个留空(占位),这就变成了一个只需要 2 个原料的新工具。</p>
<p class="maodian"></p><h2>2. 核心机制:占位符 (std::placeholders)</h2>
<p>要使用 <code>std::bind</code>,必须先认识它的搭档:<strong>占位符</strong>。</p>
<p>它们位于 <code>std::placeholders</code> 命名空间下,通常写成 <code>_1</code>, <code>_2</code>, <code>_3</code>...</p>
<ul><li><code>_1</code>:代表新函数被调用时的<strong>第 1 个</strong>参数。</li><li><code>_2</code>:代表新函数被调用时的<strong>第 2 个</strong>参数。</li><li>以此类推。</li></ul>
<p class="maodian"></p><h2>3. 代码实战</h2>
<p>我们需要包含头文件:</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;functional&gt;
using namespace std::placeholders; // 方便使用 _1, _2</pre></div>
<p class="maodian"></p><h3>场景 A:固定参数(减少参数个数)</h3>
<p>假设你有一个减法函数,你希望得到一个&ldquo;减去 10&rdquo;的专用函数。</p>
<div class="jb51code"><pre class="brush:cpp;">#include &lt;iostream&gt;
#include &lt;functional&gt;
using namespace std::placeholders;
int sub(int a, int b) {
    return a - b;
}
int main() {
    // 使用 bind:
    // 我们把 sub 的第二个参数 b 固定死为 10
    // _1 代表新函数 sub_ten 的第一个参数,它会被填入 sub 的第一个位置 a
    auto sub_ten = std::bind(sub, _1, 10);
    // 调用新函数:
    // 实际执行的是: sub(20, 10)
    std::cout &lt;&lt; sub_ten(20) &lt;&lt; std::endl; // 输出 10
    return 0;
}</pre></div>
<p class="maodian"></p><h3>场景 B:参数重排(改变参数顺序)</h3>
<p><code>std::bind</code> 甚至可以颠倒参数的顺序。</p>
<div class="jb51code"><pre class="brush:cpp;">// 还是上面的 sub(a, b) -&gt; a - b
// 这次我们交换位置:
// sub 的第一个参数 (a) 吃新函数的第二个参数 (_2)
// sub 的第二个参数 (b) 吃新函数的第一个参数 (_1)
auto reverse_sub = std::bind(sub, _2, _1);
// 调用: reverse_sub(10, 20)
// 实际执行: sub(20, 10) -&gt; 20 - 10
std::cout &lt;&lt; reverse_sub(10, 20) &lt;&lt; std::endl; // 输出 10</pre></div>
<p class="maodian"></p><h3>场景 C:绑定成员函数(最常用的旧式写法)</h3>
<p>这是 <code>std::bind</code> 在 C++11 初期最常见的用途:配合 <code>std::function</code> 做回调,特别是为了绑定 <code>this</code> 指针。</p>
<div class="jb51code"><pre class="brush:cpp;">class Hero {
public:
void heal(int amount) {
    std::cout &lt;&lt; "Healed " &lt;&lt; amount &lt;&lt; " hp." &lt;&lt; std::endl;
}
};
int main() {
    Hero myHero;
    // 绑定成员函数
    // 参数1:成员函数指针 &amp;Hero::heal
    // 参数2:对象实例的地址 &amp;myHero (作为隐式的 this)
    // 参数3:占位符 _1 (将来传进来的 amount)
    std::function&lt;void(int)&gt; func = std::bind(&amp;Hero::heal, &amp;myHero, _1);
    func(50); // 输出: Healed 50 hp.
}</pre></div>
<p class="maodian"></p><h2>4. 这里的坑:参数拷贝 vs 引用</h2>
<p>这是 <code>std::bind</code> 最容易出错的地方。<code><strong>std::bind</strong></code><strong> 默认是按值拷贝参数的!</strong></p>
<p>如果你想绑定的参数是一个引用,或者是一个无法拷贝的对象</p>
<p>(比如 <code>unique_ptr</code>),你必须显式使用 <code><strong>std::ref</strong></code> 或 <code><strong>std::cref</strong></code> (const ref)。</p>
<div class="jb51code"><pre class="brush:cpp;">void updateScore(int&amp; score, int val) {
    score += val;
}
int main() {
    int myScore = 100;
    // 错误写法!
    // auto update = std::bind(updateScore, myScore, _1);
    // 这会导致 bind 内部拷贝了一份 myScore,原本的 myScore 不会变。
    // 正确写法:使用 std::ref
    auto update = std::bind(updateScore, std::ref(myScore), _1);
    update(20);
    std::cout &lt;&lt; myScore &lt;&lt; std::endl; // 输出 120
}</pre></div>
<p class="maodian"></p><h2>5. 灵魂拷问:为什么现在不推荐用std::bind?</h2>
<p>既然 Lambda 讲完了,Bind 也讲完了,我们对比一下。现在 C++ 社区(包括官方指南)强烈建议<strong>能用 Lambda 就别用 Bind</strong>。</p>
<p>原因如下:</p>
<ol><li><strong>可读性差</strong>:<code>bind(func, _2, 10, _1)</code> 这种写法像是猜谜语,你需要脑补参数填空的对应关系。而 Lambda <code>[](int x, int y) { return func(y, 10, x); }</code> 一目了然。</li><li><strong>容易出错</strong>:刚才提到的 <code>std::ref</code> 问题,很容易忘记写,导致很多隐晦的 Bug。而 Lambda 的捕获列表 <code>[&amp;]</code> 显式且直观。</li><li><strong>编译器优化</strong>:Lambda 表达式对编译器来说是透明的代码块,容易内联优化。<code>std::bind</code> 生成的是复杂的模板对象,编译器优化起来比较费劲,有时会导致生成的二进制代码更大、更慢。</li></ol>
<p><strong>对比示例:</strong></p>
<div class="jb51code"><pre class="brush:cpp;">// 任务:调用 object.process(data)
// 方式 1: std::bind (旧时代)
auto task1 = std::bind(&amp;MyClass::process, &amp;object, data);
// 方式 2: Lambda (新时代 - 推荐)
auto task2 = [&amp;object, data]() { object.process(data); };</pre></div>
<p></p>
<p>到此这篇关于C++中的bind代码实践的文章就介绍到这了,更多相关C++ bind内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>C++/Python混合编程之小结Pybind11的使用</li><li>python使用Pybind11扩展c++的实现</li><li>C++11&nbsp;std::function和std::bind&nbsp;的使用示例详解</li><li>C++ ROS与boost:bind()使用详解</li><li>C++11中bind绑定器和function函数对象介绍</li><li>c++中的bind使用方法</li><li>C++ std::bind用法详解</li><li>C++中使用function和bind绑定类成员函数的方法详解</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: C++中的bind实践代码