CPP/bind

From ProgrammingExamples
< CPP
Jump to: navigation, search

bind.cpp

#include <iostream>
#include <functional>
 
void add(int a, int b)
{
    std::cout << "a: " << a << " b: " << b << std::endl;
}
 
int main(int,char*[])
{
    // free function, full call
    {
    std::function<void(int, int)> f = std::bind(add, std::placeholders::_1, std::placeholders::_2);
    f(1,2);
    }
 
    // free function, partial-arg call
    {
    std::function<void(int)> f = std::bind(add, 1, std::placeholders::_1);
    f(2);
    }
 
    // free function, no-arg call
    {
    std::function<void()> f = std::bind(add, 1, 2);
    f();
    }
 
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(bind)
 
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
 
ADD_EXECUTABLE(bind bind.cpp)