简体   繁体   English

使用boost :: spirit的代码中歧义类型的编译错误

[英]Compilation error on ambiguous type in code using boost::spirit

If I write this code it still compiles: 如果我编写这段代码,它仍然可以编译:

namespace MyNamespace
{

struct STreeConstructionRuleQuery : std::string {};
struct STreeConstructionRuleOperation : std::string {};
struct STreeConstructionRuleOperand : std::string {};
struct STreeConstructionRuleCondition : std::string {};

struct STreeConstructionRuleOperationWithOperands : boost::tuple<STreeConstructionRuleOperation, std::vector<STreeConstructionRuleOperand> > {};

struct STreeConstructionRule
{
    boost::optional<std::vector<std::vector<STreeConstructionRuleOperationWithOperands> > > m_sAssertion;
    STreeConstructionRuleQuery m_sQuery;
    STreeConstructionRuleOperationWithOperands m_sAction;
    boost::optional<STreeConstructionRuleCondition> m_sCondition;
};

}


BOOST_FUSION_ADAPT_STRUCT(
    MyNamespace::STreeConstructionRule,
    (boost::optional<std::vector<std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> > >, m_sAssertion)
    (MyNamespace::STreeConstructionRuleQuery, m_sQuery)
    (MyNamespace::STreeConstructionRuleOperationWithOperands, m_sAction)
    (boost::optional<MyNamespace::STreeConstructionRuleCondition>, m_sCondition)
)


namespace MyNamespace
{

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

template <typename Iterator>
struct STreeContructionRulesGrammar : qi::grammar<Iterator, std::vector<STreeConstructionRule>(), ascii::space_type>
{
    qi::rule<Iterator, std::vector<STreeConstructionRule>(), ascii::space_type> m_oStart;
    qi::rule<Iterator, qi::unused_type(), ascii::space_type> m_oComment;
    qi::rule<Iterator, STreeConstructionRule(), ascii::space_type> m_oRule;

    STreeContructionRulesGrammar() : STreeContructionRulesGrammar::base_type(m_oStart)
    {
        m_oStart = *(m_oComment | m_oRule [phoenix::push_back(qi::_val, qi::_1)]);
        m_oComment = (("-->" >> *(qi::char_) >> "<--") | ('#' >> *(qi::char_ - qi::char_("\n\r")))); 
    }

};
}

But when I change the definition of STreeConstructionRuleOperand to 但是当我将STreeConstructionRuleOperand的定义STreeConstructionRuleOperand

struct STreeConstructionRuleOperand : boost::variant<int> {};

I get the following compilation error (STLPort is used instead of STL): 我收到以下编译错误(使用STLPort而不是STL):

1>d:\commonlib\include\boost\variant\variant.hpp(1373) : error C2666: 'boost::variant<T0_>::convert_construct' : 2 overloads have similar conversions
1>        with
1>        [
1>            T0_=int
1>        ]
1>        d:\commonlib\include\boost\variant\variant.hpp(1358): could be 'void boost::variant<T0_>::convert_construct<int,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_,boost::detail::variant::void_>(const boost::variant<T0_> &,long)'
1>        with
1>        [
1>            T0_=int
1>        ]
1>        d:\commonlib\include\boost\variant\variant.hpp(1289): or       'void boost::variant<T0_>::convert_construct<const T>(T &,int,boost::mpl::false_)'
1>        with
1>        [
1>            T0_=int,
1>            T=MyNamespace::STreeConstructionRuleOperand
1>        ]
1>        while trying to match the argument list '(const MyNamespace::STreeConstructionRuleOperand, long)'
1>        d:\commonlib\include\boost\fusion\container\vector\detail\vector_n.hpp(45) : see reference to function template instantiation 'boost::variant<T0_>::variant<MyNamespace::STreeConstructionRuleOperand>(const T &)' being compiled
1>        with
1>        [
1>            T0_=int,
1>            T=MyNamespace::STreeConstructionRuleOperand
1>        ]
1>        d:\commonlib\include\stlport\stl\_uninitialized.h(93) : see reference to function template instantiation 'void stlpd_std::_Destroy_Range<_OutputIter>(_ForwardIterator,_ForwardIterator)' being compiled
1>        with
1>        [
1>            _OutputIter=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *,
1>            _ForwardIterator=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *
1>        ]
1>        d:\commonlib\include\stlport\stl\_uninitialized.h(113) : see reference to function template instantiation '_OutputIter stlpd_std::priv::__ucopy<_InputIter,_OutputIter,ptrdiff_t>(_RandomAccessIter,_RandomAccessIter,_OutputIter,const stlpd_std::random_access_iterator_tag &,_Distance *)' being compiled
1>        with
1>        [
1>            _OutputIter=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *,
1>            _InputIter=const stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *,
1>            _RandomAccessIter=const stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *,
1>            _Distance=ptrdiff_t
1>        ]
1>        d:\commonlib\include\stlport\stl\_vector.h(249) : see reference to function template instantiation '_OutputIter stlpd_std::priv::__ucopy_ptrs<const stlpd_std::vector<_Tp>*,stlpd_std::vector<_Tp>*>(_InputIter,_InputIter,_OutputIter,const stlpd_std::__false_type &)' being compiled
1>        with
1>        [
1>            _OutputIter=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *,
1>            _Tp=MyNamespace::STreeConstructionRuleOperationWithOperands,
1>            _InputIter=const stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands> *
1>        ]
1>        d:\commonlib\include\stlport\stl\_vector.h(246) : while compiling class template member function 'stlpd_std::priv::_NonDbg_vector<_Tp,_Alloc>::_NonDbg_vector(const stlpd_std::priv::_NonDbg_vector<_Tp,_Alloc> &)'
1>        with
1>        [
1>            _Tp=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>,
1>            _Alloc=stlpd_std::allocator<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>
1>        ]
1>        d:\commonlib\include\stlport\stl\debug\_iterator.h(378) : see reference to class template instantiation 'stlpd_std::priv::_NonDbg_vector<_Tp,_Alloc>' being compiled
1>        with
1>        [
1>            _Tp=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>,
1>            _Alloc=stlpd_std::allocator<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>
1>        ]
1>        d:\commonlib\include\stlport\stl\debug\_vector.h(106) : see reference to class template instantiation 'stlpd_std::priv::__construct_checker<_Container>' being compiled
1>        with
1>        [
1>            _Container=stlpd_std::priv::_NonDbg_vector<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>,stlpd_std::allocator<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>>
1>        ]
1>        d:\commonlib\include\boost\optional\optional.hpp(110) : see reference to class template instantiation 'stlpd_std::vector<_Tp>' being compiled
1>        with
1>        [
1>            _Tp=stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>
1>        ]
1>        d:\commonlib\include\boost\optional\optional.hpp(113) : see reference to class template instantiation 'boost::optional_detail::aligned_storage<T>::dummy_u' being compiled
1>        with
1>        [
1>            T=stlpd_std::vector<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>
1>        ]
1>        d:\commonlib\include\boost\optional\optional.hpp(450) : see reference to class template instantiation 'boost::optional_detail::aligned_storage<T>' being compiled
1>        with
1>        [
1>            T=stlpd_std::vector<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>
1>        ]
1>        d:\commonlib\include\boost\optional\optional.hpp(457) : see reference to class template instantiation 'boost::optional_detail::optional_base<T>' being compiled
1>        with
1>        [
1>            T=stlpd_std::vector<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>
1>        ]
1>        d:\preformator\sources\prefcore\preftreeconstructor.cpp(52) : see reference to class template instantiation 'boost::optional<T>' being compiled
1>        with
1>        [
1>            T=stlpd_std::vector<stlpd_std::vector<MyNamespace::STreeConstructionRuleOperationWithOperands>>
1>        ]

This is the only error the compiler reports. 这是编译器报告的唯一错误。 I don't see any ambiguity but the compiler does. 我看不出有任何歧义,但是编译器确实有。 What is the reason for this error and how it should be fixed? 此错误的原因是什么,应如何解决?

Two things strike me here: 这里有两点让我震惊:

  1. how would something that is used to be std::string , be assigned to something that is a now a variant<int> ? 如何将过去是std::string东西分配给现在为variant<int>

    The code shown doesn't include the code that would explain this (as it is probably in m_oRule or a subrule of that). 显示的代码不包含解释此问题的代码(因为它可能在m_oRule或它的子规则中)。 But perhaps a variant<std::string> poses fewer problems, seeing how you had a std::string value to assign before. 但是,也许variant<std::string>带来更少的问题,看看您之前如何分配std::string值。

  2. You appear to abuse struct inheritance quite a lot to achieve typedef behaviour. 您似乎非常滥用 struct 继承来实现typedef行为。 Note that public inheritance is not the same as a typedef , especially with regards to the constructors (which are not inherited). 需要注意的是公有继承是一样的typedef ,尤其是关于构造函数没有继承)。

    Try replacing each use of 尝试替换每次使用

     struct A : baseType {}; 

    by the more usual idiom of 通过更常见的成语

     typedef baseType A; 

I actually expect the latter to solve the issue, since you are running into constructor overload trouble. 我实际上希望后者能够解决问题,因为您遇到了构造函数重载的麻烦。

Regardless of that, I have only tested with gcc, boost_1_51_0 and GNU libstdc++. 无论如何,我仅使用gcc,boost_1_51_0和GNU libstdc ++进行了测试。 It compiles everything you posted without a hitch (but again, not you left out crucial elements, such as the rule m_oRule that does the actual assigning to RuleOpWithOperands ). 它可以轻松编译您发布的所有内容(但同样,您也可以保留重要的元素,例如规则 m_oRule ,该元素实际分配给RuleOpWithOperands )。

Demonstration FauxTypedef: 演示FauxTypedef:

Demonstrating the relevant problem (at least one) with inheritance instead of typedef : 继承而不是typedef演示相关问题(至少一个):

struct Type {
    Type(int, double) { }
};

typedef Type          ProperTypedef;
struct  FauxTypedef : Type { };

int main()
{
    Type          a(1, 4.2); // okay
    ProperTypedef b(1, 4.2); // okay as well, obviously
    FauxTypedef   c(1, 4.2); // oops ouch!!! This won't compile
}

If you insist on having a _unique typeid for the typedef-ed name, you can resort to BOOST_STRONG_TYPEDEF 如果您坚持使用_unique typeid作为类型定义名称,则可以使用BOOST_STRONG_TYPEDEF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM