开发者

How to iterate through such a map? How to fix 8 scary C2784 errors?

开发者 https://www.devze.com 2023-03-20 16:45 出处:网络
So I try to compile such code: bool server_utils::find_service_by_name_iterator_function(std::pair<boost::shared_ptr<service>, server_utils::service_description> const & element, std:

So I try to compile such code:

bool server_utils::find_service_by_name_iterator_function(std::pair<boost::shared_ptr<service>, server_utils::service_description> const & element, std::string name) const
{
        return element.second.name == name;
}

server_utils::service_description server_utils::stop_service_by_name(std::string name)
{
    typedef std::map<boost::shared_ptr<service>, server_utils::service_description> map_t;
    map_t::iterator map_it =   std::find_if(description.service_map.begin(), description.service_map.end(), std::bind1st(std::ptr_fun(&server_utils::find_service_by_name_iterator_function), name)); 

    if (map_it != description.service_map.end())
    {
         description.service_map.erase (map_it);
    } 
    else
    {
    throw std::runtime_error("Service with such name was not found map not found!");
    }
}

where

struct service_description
    {
        //A service must have
        std::string name;
        std::string library_name;
        std::string class_name;
        std::string root_file_system_directory;
        boost::property_tree::ptree service_custome_properties_tree;

        //A service might have
        std::vector<std::string> set_of_url_rules;
        boost::unordered_multimap<std::string, std::string> set_of_header_rules;
        boost::unordered_multimap<std::string, std::string> set_of_arguments_rules;
        std::set<std::string> url_extensions;
        std::string root_service_web_path;  
    };

But I get strange errors:

error C2784: 'std::pointer_to_binary_function<_Arg1,_Arg2,_Result,_Result(__cdecl *)(_Arg1,_Arg2)> std::ptr_fun(_Result (__cdecl *)(_Arg1,_Arg2))' : could not deduce template argument for '_Result (__cdecl *)(_Arg1,_Arg2)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'

error C2784: 'std::pointer_to_binary_function<_Arg1,_Arg2,_Result,_Result(__fastcall *)(_Arg1,_Arg2)> std::ptr_fun(_Result (__fastcall *)(_Arg1,_Arg2))' : could not deduce template argument for '_Result (__fastc开发者_开发百科all *)(_Arg1,_Arg2)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'   

error C2784: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__cdecl *)(_Arg)> std::ptr_fun(_Result (__cdecl *)(_Arg))' : could not deduce template argument for '_Result (__cdecl *)(_Arg)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const' 

error C2784: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__stdcall *)(_Arg)> std::ptr_fun(_Result (__stdcall *)(_Arg))' : could not deduce template argument for '_Result (__stdcall *)(_Arg)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'   

and so on...

What shall I do? How to fix my code? can boost.function or boost.bind help me?


Use boost::bind; it's far smarter:

map_t::iterator map_it = std::find_if(
      description.service_map.begin(), description.service_map.end(),
      boost::bind(&server_utils::find_service_by_name_iterator_function, this, name));


server_utils::find_service_by_name_iterator_function needs to be made static.

The errors are complaining that it is a class method (__thiscall) not a function (__stdcall, __cdecl, etc).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号