名稱空間
變體
操作

std::binder1st, std::binder2nd

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直至 C++17*)(直至 C++17*)(直至 C++17*)(直至 C++17*)
(直到 C++20*)
(直到 C++20*)
binder1stbinder2nd
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直至 C++17*)(直至 C++17*)(直至 C++17*)(直至 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <functional>
template< class Fn >

class binder1st
    : public std::unary_function<typename Fn::second_argument_type,
                                 typename Fn::result_type> {
protected:
    Fn op;
    typename Fn::first_argument_type value;
public:
    binder1st( const Fn& fn,
               const typename Fn::first_argument_type& value );

    typename Fn::result_type
        operator()(const typename Fn::second_argument_type& x) const;

    typename Fn::result_type
        operator()(typename Fn::second_argument_type& x) const;

};
(1) (C++11 起已棄用)
(在 C++17 中已移除)
template< class Fn >

class binder2nd
    : public std::unary_function<typename Fn::first_argument_type,
                                 typename Fn::result_type> {
protected:
    Fn op;
    typename Fn::second_argument_type value;
public:
    binder2nd( const Fn& fn,
               const typename Fn::second_argument_type& value );

    typename Fn::result_type
        operator()(const typename Fn::first_argument_type& x) const;

    typename Fn::result_type
        operator()(typename Fn::first_argument_type& x) const;

};
(2) (C++11 起已棄用)
(在 C++17 中已移除)

將實參繫結到二元函式(binary function)的函式物件。

引數值在物件構造時傳入並存儲於物件內部。當函式物件透過 operator() 被呼叫時,儲存的值將作為其中一個實參,另一個實參作為 operator() 的實參傳入。結果函式物件為一元函式(unary function)。

1) 將第一個引數繫結到在物件構造時給定值 value
2) 將第二個引數繫結到在物件構造時給定值 value

[編輯] 示例

#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
 
const double pi = std::acos(-1); // use std::numbers::pi in C++20
 
int main()
{
    // deprecated in C++11, removed in C++17
    auto f1 = std::bind1st(std::multiplies<double>(), pi / 180.0);
 
    // C++11 replacement
    auto f2 = [](double a) { return a * pi / 180.0; };
 
    for (double n : {0, 30, 45, 60, 90, 180})
        std::cout << n << \t" << std::fixed << "= "
                  << f1(n) << " rad (using binder)\t= "
                  << f2(n) << " rad (using lambda)\n"
                  << std::defaultfloat;
}

輸出

0°	= 0.000000 rad (using binder)	= 0.000000 rad (using lambda)
30°	= 0.523599 rad (using binder)	= 0.523599 rad (using lambda)
45°	= 0.785398 rad (using binder)	= 0.785398 rad (using lambda)
60°	= 1.047198 rad (using binder)	= 1.047198 rad (using lambda)
90°	= 1.570796 rad (using binder)	= 1.570796 rad (using lambda)
180°	= 3.141593 rad (using binder)	= 3.141593 rad (using lambda)

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 109 C++98 operator() 無法修改傳入的實參 添加了過載以處理此問題

[編輯] 參閱

(C++11 中已廢棄)(C++17 中已移除)
將一個引數繫結到二元函式
(函式模板) [編輯]