Я пытаюсь написать функтор, чтобы вызвать функцию boost с привязкой и некоторым шаблоном. Поэтому у меня есть это главное:Функция с шаблонами и boost
int function(char c)
{
std::cout << c << std::endl;
return (0);
}
int main()
{
Function<int (char)> fb = boost::bind(&function, _1);
fb('c');
return (0);
}
и это мой класс Function
:
template<typename F>
class Function
{
private:
F functor;
public:
Function()
{
this->functor = NULL;
};
Function(F func)
{
this->functor = func;
};
template <typename P>
void operator()(P arg)
{
(*this->functor)(arg);
};
void operator=(F func)
{
this->functor = func;
};
};
У меня есть проблема: когда я пытаюсь скомпилировать я эти ошибки:
error C2440: 'initializing' : cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'Function<F>'
IntelliSense: no suitable user-defined conversion from "boost::_bi::bind_t<int, int (*)(char), boost::_bi::list1<boost::arg<1>>>" to "Function<int (char)>" exists
Кто-то может мне помочь?
Подробнее о стиле Erasure в C++. Ваш класс не реализует его, тогда как 'boost :: function' и' std :: function' в C++ 11 реализуют его. Я бы рекомендовал вам прочитать эту статью: http://www.artima.com/cppsource/type_erasure.html – Nawaz