使用boost :: assign :: map_list_of填充boost :: function的std :: map

时间:2014-03-20 15:45:32

标签: c++ boost boost-bind boost-function

我想创建一个键值数据结构,这对于响应字符串匹配事件以正则表达式模式非常有用。所以我试图用Boost库来解决这个问题:

#include <boost/regex.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/assign.hpp>
#include <map>

typedef std::map<boost::regex, boost::function<void(void)> > regex_callback;

void SuOpenedCallback()
{
}

void SuClosedCallback()
{
}


int main(int argc, char** argv)
{
    //Don't pay attention to these definitions... 
    boost::regex su_opened  ("^(\\w+)\\s(\\d+)\\s([\\d:]+)\\s(\\w+)\\s"
                            "su: pam_unix\\(su:session\\): session opened for user{1}\\s([\\w\\d]+)\\sby{1}\\s([\\w\\d]+)\\(uid=([\\d]+)\\)$"); 
    boost::regex su_closed  ("^(\\w+)\\s(\\d+)\\s([\\d:]+)\\s(\\w+)\\s"
                            "su: pam_unix\\(su:session\\): session closed for user{1}\\s([\\w\\d]+)$");

    //Compilation error:
    regex_callback resolver = boost::assign::map_list_of<boost::regex, boost::function<void(void)> >
        (su_opened, boost::bind(&SuOpenedCallback))
        (su_closed, boost::bind(&SuClosedCallback));

//...

}

当我尝试编译它时(不幸的是我被迫使用相当旧的软件 - gcc 4.4.7,提升1.41,因此C ++ 11支持非常有限):

 g++ boost_regexp.cpp -o test -lboost_regex -std=c++0x

编译因此错误而失败:

  

boost_regexp.cpp: In function ‘int main(int, char**)’: boost_regexp.cpp:58: error: ambiguous overload for ‘operator=’ in ‘resolver = boost::assign::map_list_of(const Key&, const T&) [with Key = boost::regex, T = boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>](((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&)((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>*)(& boost::bind [with R = void](SuOpenedCallback))))).boost::assign_detail::generic_list<T>::operator() [with U = boost::regex, U0 = boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>, T = std::pair<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>](((const boost::regex&)((const boost::regex*)(& su_closed))), ((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>&)((const boost::_bi::bind_t<void, void (*)(), boost::_bi::list0>*)(& boost::bind [with R = void](SuClosedCallback)))))’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:251: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Tp = boost::function<void()>, _Compare = std::less<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Alloc = std::allocator<std::pair<const boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::function<void()>] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:266: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Tp = boost::function<void()>, _Compare = std::less<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Alloc = std::allocator<std::pair<const boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::function<void()>] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:286: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(std::initializer_list<std::pair<const _Key, _Tp>) [with _Key = boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Tp = boost::function<void()>, _Compare = std::less<boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, _Alloc = std::allocator<std::pair<const boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char>, boost::function<void()>] make: *** [all] Error 1

似乎我的问题接近于this one,然而,它并没有激励我。如果有人能告诉我这个错误,那就太好了。

答案正如@sehe所提到的,此代码对于gcc的最新版本是正确的,但无法使用gcc 4.4处理。不幸的是我。

1 个答案:

答案 0 :(得分:1)

我会说那应该只是编译。

你可能会尝试更直接的方法(没有绑定),这在编译器上可能(很多)更容易:

regex_callback resolver = boost::assign::map_list_of<boost::regex, boost::function<void(void)> >
    (su_opened, &SuOpenedCallback)
    (su_closed, &SuClosedCallback);

编辑由于这不起作用,请尝试使其更明确:

typedef boost::function<void(void)> Func;

regex_callback resolver = boost::assign::map_list_of<boost::regex, Func >
    (su_opened, Func(boost::bind(&SuOpenedCallback)))
    (su_closed, Func(boost::bind(&SuClosedCallback)));

// or:
regex_callback resolver = boost::assign::map_list_of<boost::regex, Func >
    (su_opened, Func(&SuOpenedCallback))
    (su_closed, Func(&SuClosedCallback));