함수 개체를 전달하는 다음 작은 프로그램에 어떤 문제가 있습니까?함수 개체 전달 : 오류
#include <iostream>
#include <functional>
void foo(const std::unary_function<const std::string&, void>& fct) {
const std::string str = "test";
fct(str); // error
}
class MyFct : public std::unary_function<const std::string&, void> {
public:
void operator()(const std::string& str) const {
std::cout << str << std::endl;
}
};
int main(int argc, char** argv){
MyFct f;
foo(f);
return 0;
}
나는 6 행에서 다음과 같은 오류를 받고 있어요 :
no match for call to
`(const std::unary_function<const std::string&, void>) (const std::string&)'
답변 해 주셔서 감사합니다. 부스트 솔루션은 템플릿 솔루션만큼 효율적입니까? – Frank
boost 함수는 래핑 된 함수 객체에 대한 간접 호출을해야하기 때문에 효율적이지 않습니다. 하지만 템플릿이 필요 없다는 장점이 있습니다. 그 boost.function을 사용하여 어딘가에 저장할 수 있습니다 (예 : 클래스 멤버). –
당신이 unary_function으로 찾은 것에 가장 가깝다고 생각합니다. –