당신은 함수의 입력으로 호출 서로 다른 유형의 개체를 사용 가능하게하는 boost::function<>
를 사용할 수 있습니다.
다음은 C++ 11을 사용하는 예제입니다 (이 예의 뒤에 설명 참조). 테스트 루틴 여기
int DoIt(float f, std::string s1, std::string s2)
{
std::cout << f << ", " << s1 << ", " << s2 << std::endl;
return 0;
}
int DoItWithFourArgs(float f, std::string s1, std::string s2, bool b)
{
std::cout << f << ", " << s1 << ", " << s2 << ", " << b << std::endl;
return 0;
}
struct X
{
int MemberDoIt(float f, std::string s1, std::string s2)
{
std::cout << "Member: " << f << ", " << s1 << ", " << s2 << std::endl;
return 0;
}
static int StaticMemberDoIt(float f, std::string s1, std::string s2)
{
std::cout << "Static: " << f << ", " << s1 << ", " << s2 << std::endl;
return 0;
}
};
그리고있다 :
int main()
{
PassFxn(DoIt); // Pass a function pointer...
// But we're not limited to function pointers with std::function<>...
auto lambda = [] (float, std::string, std::string) -> int
{
std::cout << "Hiho!" << std::endl;
return 42;
};
PassFxn(lambda); // Pass a lambda...
using namespace std::placeholders;
PassFxn(std::bind(DoItWithFourArgs, _1, _2, _3, true)); // Pass bound fxn
X x;
PassFxn(std::bind(&X::MemberDoIt, x, _1, _2, _3)); // Use a member function!
// Or, if you have a *static* member function...
PassFxn(&X::StaticMemberDoIt);
// ...and you can basically pass any callable object!
}
그리고 여기를
#include <functional>
#include <string>
#include <iostream>
void PassFxn(std::function<int(float, std::string, std::string)> func)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{
int result = func(12, "a", "b"); // call using function object
std::cout << result << std::endl;
}
이들은 그것을 테스트하는 기능의 몇 가지가 있습니다 : 이것은 당신의 기능을 다시 것입니다 방법입니다 live example입니다.
가 비고 : 당신이 실제로 (C++ 03로 작업하는 경우
가
쉽게 boost::bind<>
에 boost::function<>
및 std::bind<>
에 std::function<>
을 변경할 수 Boost.Function을가 std::function<>
영감을 나중에 표준의 일부가되었다 무엇인가 C++ 라이브러리). 이 경우 <functional>
헤더를 포함하는 대신 boost::bind
을 사용하려는 경우에만 boost/function.hpp
및 boost/bind.hpp
헤더를 포함해야합니다.
std::function<>
/boost::function<>
이 호출 가능한 객체를 캡슐화하는 기능을 제공한다는 또 다른 예를 보려면 this Q&A on StackOverflow을 참조하십시오.
당신이 찾고있는 답변의 종류에 따라 다릅니다! 물론, 위의 모든 것은'cout << DoIt (12, "a", "b") << endl;'으로 단순화 될 수 있습니다. 그러나 그것은 당신이 찾고있는 것이 아닌 것 같습니다. 즉, 함수 객체가 아닌 순수한 함수 포인터를 호출 할 때 Boost 기능을 활용할 수있는 확실한 기회가 없다는 것입니다. –
그러면 어떤 멤버 함수 나 람다 (C++ 11 호환 컴파일러가있는 경우) 일반 함수 또는 정적 멤버 함수가 아닐 수도 있습니다.물론 boost :: bind (또는 [std :: bind'] (http://en.cppreference.com/w/cpp/utility/functional/bind))를 사용하면 어떤 숫자라도 "bind"할 수 있습니다 함수 객체를 전달하기 전에 인수를 사용해야합니다. –
무엇이 고쳐지고 변경할 수 있습니까? 'PassPtr'와'DoIt'의 서명이 'no'로 고정되면 코드는 얻을 수있는만큼 간단합니다. –