std::find_if
에 대한 조건자를 boost::contains
(boost/algoritm/string 라이브러리의)과 함께 boost::bind
을 사용하여 작성하려고합니다. 다음 발췌 문장은이 작업을 수행하는 방법 두 가지를 보여줍니다. boost :: bind에 템플릿 함수 오버로드를 강제하는 방법?
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <string>
int main(int argc, char** argv) {
std::string s1("hello mom");
std::string s2("bye mom");
boost::function<bool (std::string, std::string)> f = &boost::contains<std::string, std::string>;
std::cout << s1 << " contains " << "hello, " << std::boolalpha << f(s1, "hello") << std::endl;
std::cout << s2 << " contains " << "hello, " << std::boolalpha << f(s2, "hello") << std::endl;
boost::function<bool (std::string)> contain_hello = boost::bind(boost::contains<std::string, std::string>, _1, std::string("hello"));
std::cout << s1 << " contains " << "hello, " << std::boolalpha << contain_hello(s1) << std::endl;
std::cout << s2 << " contains " << "hello, " << std::boolalpha << contain_hello(s2) << std::endl;
return EXIT_SUCCESS;
}
I 출력 다음 받고 있어요 3.4.5 ++ g이 코드를 컴파일.
error: conversion from `<unresolved overloaded function type>' to non-scalar type `boost::function<bool()(std::string, std::string), std::allocator<void> >' requested
error: no matching function for call to `bind(<unresolved overloaded function type>, boost::arg<1>&, std::string)'
내가 잘 작동 everyting 하나의 과부하를 가지고 boost::icontains
로 전환합니다. 템플릿이 아닌 함수가 여러 번 오버로드되는 경우 비슷한 상황을 해결하는 방법을 알고 있습니다. 누군가가 이것을 올바르게 쓸 수있게 도와 줄 수 있습니까? 아니면 내 자신의 비교 함수를 작성해야합니까?
작은 교정 (적어도 g ++ 3.4.5 이상) static_cast (boost :: contains <..>). 이제 완벽하게 작동합니다. 빠른 힌트를 주셔서 감사합니다. –
lollinus
감사. 그 대답을 업데이트했습니다. 필자는 멤버 함수 ptrs와 함께이 함수를 사용했기 때문에 일반적인 func-ptr 구문이 약간 녹슬 었습니다. ;) – Macke
템플릿의 PITA 성격에 대한 솔직함과 과부하는 매우 안심입니다. 그것을보기 전에, 나는 틀린 일을하고 있었음에 틀림 없다고 생각했습니다. –