1 분 소요

개요

  • 인자로 전달된 함수에 대한 전달 호출 래퍼를 생성하는 함수
  • bind_front
    • C++20
    • 함수의 첫번째 매개변수부터 바인드


예제

  • 코드
    #include <functional>
    #include <iostream>

    using namespace std;

    int main() {
    	auto test1 = []() {
    		auto plus = [](int x, int y, int z) { return x + y + z; };

    		auto bind1 = bind_front(plus, 1);
    		cout << bind1(2, 3) << endl;

    		auto bind2 = bind_front(plus, 1, 2);
    		cout << bind2(3) << endl;

    		auto bind3 = bind_front(plus, 1, 2, 3);
    		cout << bind3() << endl;
    	};
    	test1();

    	cout << endl << "------" << endl << endl;

    	auto test2 = []() {
    		class Test {
    			private:
    				int i;

    			public:
    				Test(int i) : i(i) {}
    				int plus(int value) { return this->i + value; }

    				void SetI(int i) { this->i = i; };
    		};

    		auto bind1 = bind_front(mem_fn(&Test::plus), Test(1));
    		cout << bind1(10) << endl;

    		cout << "~~~~~~" << endl;

    		Test t1(1);
    		auto bind2 = bind_front(mem_fn(&Test::plus), t1);
    		cout << bind2(10) << endl;
    		t1.SetI(-1);
    		cout << bind2(10) << endl;

    		cout << "~~~~~~" << endl;

    		Test t2(1);
    		auto bind3 = bind_front(mem_fn(&Test::plus), &t2);
    		cout << bind3(10) << endl;
    		t2.SetI(-1);
    		cout << bind3(10) << endl;
    	};
    	test2();

    	return 0;
    }
  • 실행 결과
    6
    6
    6

    ------

    11
    ~~~~~~
    11
    11
    ~~~~~~
    11
    9