【问题标题】:Populating the std::map of boost::function with boost::assign::map_list_of用 boost::assign::map_list_of 填充 boost::function 的 std::map
【发布时间】:2014-03-20 15:45:32
【问题描述】:

我想创建一个键值数据结构,该结构可用于响应字符串匹配正则表达式模式的事件。所以我正在尝试使用 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,boost 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 很接近,但是,它并没有激发我的灵感。如果有人能告诉我这个错误,那就太好了。

ANSWER 正如@sehe 所提到的,此代码对于最新版本的 gcc 是正确的,但不能使用 gcc 4.4 处理。对我来说很不幸。

【问题讨论】:

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


    【解决方案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));
    

    【讨论】:

    • 不幸的是,这会使编译崩溃并出现几乎相同的错误。
    • 但这并没有崩溃。我只能说,忍受它或升级编译器:(
    • @VitalyIsaev 添加了另一个绝望尝试的解决方法:)
    • 感谢您提供的示例。不幸的是,我过时的 gcc 无法编译其中任何一个,但 gcc 4.8.2 可以正确编译。不幸的是,我无法更新我的工作站,所以我会尝试寻找其他解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多