C++ 개념 정리

33. std::function

CE : 하랑 2026. 3. 2. 15:26

 

std::function<>는 함수, 객체의 함수, 람다 등을 담을 수 있도록 만들어진 템플릿 객체이다.

std::function -> 인자값으로 다양하게 받아준다.

 

 

 

1. 람다 

함수 객채와는 다르게 class를 선언할 필요가 없다.

즉, 코드의 길이가 줄어든다.

 

람다는 이름은 없지만 고유한 객체 입니다. -> 프로그래머는 람다의 이름과 어떤 타입으로 정의 되었는지 알 수 없지만 컴파일 과정에서 람다라는 객체를 생성한다는 말입니다. -> 때문에 람다가 인라인화 가능한 이유입니다.

많이 사용하는 람다 캡처 표현식

[]

[=]

[&]

#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <string_view>
#include <functional>

class A
{
public:
    std::function<void()> Start = nullptr; // 함수 담는 그릇 이라고 보면 될듯
    std::function<void(int)> Updata = nullptr;

    void SetFunctionint(std::function<void(int)> _FUnction)
    {
        Updata = _FUnction;
    }

    void SetFUnctionvoid(std::function<void()> _Function)
    {
        Start = _Function;
    }


    void startFun() // std::function에 담긴 함수 실행
    {
        if (Start != nullptr)
        {
            Start();
        }

        if (Updata != nullptr)
        {
            Updata(0);
        }
    }
};

class functioncalss
{
public:
    void test1()
    {
        std::cout << "함수포인터 첫 시작"<<"\n";
    }

    void test2(int _text)
    {
        std::cout << _text << "\n";
    }
};
// 큐처럼 사용해보기 FIFO 선입선출
int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    std::shared_ptr<functioncalss> testFun = nullptr;
    std::shared_ptr<A> a = nullptr;

    testFun = std::make_shared<functioncalss>();
    a = std::make_shared<A>();

    //a->SetFUnctionvoid([=]() {testFun->test1(); }); 
    //a->SetFunctionint([=](int) {testFun->test2(12); });
    // 
    // 인자가 없는 void() 면 [=], [=]() 표현 가능
    // &, = 정리해서 공부하기 
    a->Start = [&] {testFun->test1(); };
    a->Updata = [=](int) {testFun->test2(12); };

    a->startFun();
}

 

2. std::bind

 std::bind는 함수나 객체의 함수를 생성하고 반환하는 함수입니다.

#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <string_view>
#include <functional>

class A
{
public:
    std::function<void()> Start = nullptr; // 함수 담는 그릇 이라고 보면 될듯
    std::function<void(int)> Updata = nullptr;

    void SetFunctionint(std::function<void(int)> _FUnction)
    {
        Updata = _FUnction;
    }

    void SetFUnctionvoid(std::function<void()> _Function)
    {
        Start = _Function;
    }


    void startFun(int _text) // std::function에 담긴 함수 실행
    {
        if (Start != nullptr)
        {
            Start();
        }

        if (Updata != nullptr)
        {
            Updata(_text);
        }
    }
};

class functioncalss
{
public:
    void test1()
    {
        std::cout << "함수포인터 첫 시작"<<"\n";
    }

    void test2(int _text)
    {
        std::cout << _text << "\n";
    }
};
// 큐처럼 사용해보기 FIFO 선입선출
int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    std::shared_ptr<functioncalss> testFun = nullptr;
    std::shared_ptr<A> a = nullptr;

    testFun = std::make_shared<functioncalss>();
    a = std::make_shared<A>();
  
    a->SetFUnctionvoid(std::bind(&functioncalss::test1, testFun));
    //a->SetFunctionint(std::bind(&functioncalss::test2, testFun, 100)); 100 그냥 박아도 됨
    a->SetFunctionint(std::bind(&functioncalss::test2, testFun, std::placeholders::_1));
    // std::placeholders -> bind 된 함수의 인자 값을 바꿀수 있게 해준다.

    a->startFun(12);
    a->startFun(100);
}

 

 

 

 


 

std::bind보다 Lambda 선호 하는 이유?

- 가독성 더 읽기 좋다?

- 인라인화 -> 컴파일러에서 실행 -> 효율성?

 

 

std::placeholders :: _1 -> std::bind 와 같이 사용

- bind시 함수의 인자를 변수로 변경하고 싶을 때 사용

- _1 -> 인자의 순서

 

 

'C++ 개념 정리' 카테고리의 다른 글

32. memcpy  (0) 2026.03.02
31. 파일 입출력  (0) 2026.03.02
30. filesystem  (0) 2026.03.02
29. namespace  (0) 2026.03.02
27. static VS extern  (0) 2026.03.01