Estoy intentando escribir una función que tomará un funtor como argumento, invocará el funtor y luego devolverá su valor devuelto en boost::shared_ptr
."<class name> no proporciona un operador de llamada" error al intentar ajustar el valor de retorno de la función
El siguiente se niega a compilar y estoy libre de ideas. Obtengo "std :: vector < std :: string> no proporciona un operador de llamada" (más o menos). Estoy usando Clang 3.1 en Mac OS X.
template< typename T >
boost::shared_ptr<T> ReturnValueAsShared(
boost::function< T() > func)
{
return boost::make_shared<T>(func());
}
Este es el contexto en el que estoy tratando de usarlo:
make_shared< packaged_task< boost::shared_ptr< std::vector<std::string> > > >(
bind(ReturnValueAsShared< std::vector<std::string> >,
bind([a function that returns a std::vector<std::string>])));
EDIT: He aquí un caso completo de prueba autónomo. Este código falla al compilar con el mismo error, y para la vida de mí no puedo ver lo que está mal:
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>
#include <vector>
std::vector<std::string> foo(std::string a)
{
std::vector<std::string> vec;
vec.push_back(a);
return vec;
}
template< typename T >
boost::shared_ptr<T> ReturnValueAsShared(
boost::function< T() > func)
{
return boost::make_shared<T>(func());
}
int main()
{
auto f = boost::bind(ReturnValueAsShared< std::vector<std::string> >,
boost::bind(foo, std::string("a")));
f();
} // main
Y aquí está la salida de error:
In file included from testcase.cpp:3:
In file included from /usr/local/include/boost/function.hpp:64:
In file included from /usr/local/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47:
In file included from /usr/local/include/boost/function/detail/function_iterate.hpp:14:
In file included from /usr/local/include/boost/function/detail/maybe_include.hpp:13:
/usr/local/include/boost/function/function_template.hpp:132:18: error: type 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >' does not provide a call operator
return (*f)(BOOST_FUNCTION_ARGS);
^~~~
/usr/local/include/boost/function/function_template.hpp:907:53: note: in instantiation of member function 'boost::detail::function::function_obj_invoker0<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >::invoke' requested here
{ { &manager_type::manage }, &invoker_type::invoke };
^
/usr/local/include/boost/function/function_template.hpp:722:13: note: in instantiation of function template specialization 'boost::function0<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >::assign_to<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >' requested here
this->assign_to(f);
^
/usr/local/include/boost/function/function_template.hpp:1042:5: note: in instantiation of function template specialization 'boost::function0<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >::function0<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >' requested here
base_type(f)
^
/usr/local/include/boost/bind/bind.hpp:243:43: note: in instantiation of function template specialization 'boost::function<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >()>::function<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >' requested here
return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]);
^
/usr/local/include/boost/bind/bind_template.hpp:20:27: note: in instantiation of function template specialization 'boost::_bi::list1<boost::_bi::bind_t<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > (*)(std::basic_string<char>), boost::_bi::list1<boost::_bi::value<std::basic_string<char> > > > >::operator()<boost::shared_ptr<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, boost::shared_ptr<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > > (*)(boost::function<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >()>), boost::_bi::list0>' requested here
BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
^
testcase.cpp:27:4: note: in instantiation of member function 'boost::_bi::bind_t<boost::shared_ptr<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > >, boost::shared_ptr<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > > (*)(boost::function<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >()>), boost::_bi::list1<boost::_bi::bind_t<std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >, std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > (*)(std::basic_string<char>), boost::_bi::list1<boost::_bi::value<std::basic_string<char> > > > > >::operator()' requested here
f();
^
1 error generated.
Aquí hay algunas pistas más. El código siguiente se compila bien, pero eso no me ayuda ya que este no es el código que quiero :)
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>
#include <vector>
std::vector<std::string> foo()
{
std::vector<std::string> vec;
return vec;
}
template< typename T >
boost::shared_ptr<T> ReturnValueAsShared(
boost::function< T() > func)
{
return boost::make_shared<T>(func());
}
int main()
{
auto f = boost::bind(ReturnValueAsShared< std::vector<std::string> >,
foo);
f();
} // main
Simplifique el problema y proporcione un código de ejemplo mínimo (que no funcione). –
¿Cuál es el error? –
@KerrekSB El error es 'std :: vector no proporciona un operador de llamada' cuando intento usarlo como' ReturnValueAsShared > '. –
Lucas