【问题标题】:Confusion between boost::bind and boost::phoenix placeholdersboost::bind 和 boost::phoenix 占位符之间的混淆
【发布时间】:2011-03-27 16:25:34
【问题描述】:

boost::spirit 文档有这个重要警告

为 Spirit.Qi 编写语义动作有多种方式:使用 普通函数,Boost.BindBoost.LambdaPhoenix。后者 三允许您使用特殊的占位符来控制参数 展示位置(_1_2 等)。这些库中的每一个都有自己的 占位符的实现,都在不同的命名空间中。你有 确保不要将占位符与它们不属于的库混合 并且在编写语义动作时不要使用不同的库。

一般来说,对于Boost.Bind,使用::_1::_2等(是的,这些占位符是 在全局命名空间中定义)。

对于Boost.Lambda,使用命名空间boost::lambda 中定义的占位符。

对于使用 Phoenix 编写的语义动作,使用定义在 命名空间boost::spirit。请注意,所有现有的占位符 您也可以从命名空间boost::spirit::qi 获得方便

(documentation)

好的,所以我写了这段代码

template <typename Iterator>
struct ruleset_grammar : qi::grammar<Iterator>
{
    template <typename TokenDef>
    ruleset_grammar(TokenDef const& tok)
      : ruleset_grammar::base_type(start)
    {

        start =  *(  tok.set_name [ boost::bind( &cRuleSet::setName, &theRuleSet, ::_1 ) ]
                  )
              ;
    }

    qi::rule<Iterator> start;
};

请注意::_1的使用

但是,我仍然得到这个编译器错误

c:\documents and settings\james\spirit_test.cpp(138) : error C2872: '_1' : ambiguous symbol
        could be 'c:\program files\boost\boost_1_44\boost\spirit\home\support\argument.hpp(124) : const boost::phoenix::actor<Eval> boost::spirit::_1'
        with
        [
            Eval=boost::spirit::argument<0>
        ]
        or       'c:\program files\boost\boost_1_44\boost\bind\placeholders.hpp(43) : boost::arg<I> `anonymous-namespace'::_1'
        with
        [
            I=1
        ]

如何修复这个编译器错误?

【问题讨论】:

    标签: c++ boost boost-spirit


    【解决方案1】:

    您是否在该文件顶部的某处写了using namespace boost::spirit;?因为如果是,那么精神和绑定占位符现在都在全局命名空间中。直接使用qi:: 可能支持我的假设,但这也可能是一个简单的namespace qi = boost::spirit::qi

    【讨论】:

    • 我认为你是对的。当然,删除它会给我带来一百万个其他错误。我会回复你的。
    • @ravenspoint:如果您确实在某处有 using namespace boost::spirit,只需将其替换为 namespace qi = boost::spirit::qi; namespace karma = boost::spirit::karma 等以获取所有需要的命名空间。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多