使用重复将Boost Spirit Qi存储到std :: vector中会导致模糊的类模板实例化

Bar*_*adé 1 c++ boost-spirit

将重复语句的结果存储到std :: vector会导致编译错误:

/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:172:12: error: ambiguous 

    class template instantiation for ‘struct boost::spirit::qi::detail::pass_through_container_base<std::vector<Vertex3d<float> >, Vertex3d<float>, Vertex3d<float>, mpl_::bool_<false>, void>’

/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:103:12: error: candidates are: struct boost::spirit::qi::detail::pass_through_container_base<Container, ValueType, Attribute, Sequence, typename boost::enable_if<boost::fusion::traits::is_sequence<Attribute> >::type>
     struct pass_through_container_base<Container, ValueType, Attribute
            ^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:136:12: error:                 struct boost::spirit::qi::detail::pass_through_container_base<Container, ValueType, Attribute, Sequence, typename boost::enable_if<boost::spirit::traits::is_container<T2> >::type>
     struct pass_through_container_base<
            ^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:172:12: error: invalid use of incomplete type ‘struct boost::spirit::qi::detail::pass_through_container_base<std::vector<wc3lib::Vertex3d<float> >, wc3lib::Vertex3d<float>, wc3lib::Vertex3d<float>, mpl_::bool_<false>, void>’
     struct pass_through_container
            ^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:50:12: error: declaration of ‘struct boost::spirit::qi::detail::pass_through_container_base<std::vector<wc3lib::Vertex3d<float> >, wc3lib::Vertex3d<float>, wc3lib::Vertex3d<float>, mpl_::bool_<false>, void>’
     struct pass_through_container_base
Run Code Online (Sandbox Code Playgroud)

以下代码用于语法:

qi::rule<Iterator, long32(), Skipper> integer_literal;
qi::rule<Iterator, float32(), Skipper> real_literal;
qi::rule<Iterator, VertexReal3d(), Skipper> vertex_real_3d;
qi::rule<Iterator, Geoset::Vertices(), Skipper, qi::locals<long32> > vertices;


integer_literal %=
        lexeme[
            qi::int_parser<long32>()
        ]
    ;

real_literal %=
        lexeme[
            qi::real_parser<float32>()
        ]
    ;

vertex_real_3d =
        lit('{')
        >> real_literal[at_c<0>(_val) = _1]
        >> lit(',')
        >> real_literal[at_c<1>(_val) = _1]
        >> lit(',')
        >> real_literal[at_c<2>(_val) = _1]
        >> lit('}')
    ;

vertices =
        lit("Vertices")
        >> integer_literal[_a = _1]
        >> lit('{')
        >> repeat(_a)[
            vertex_real_3d
            >> lit(',')
        ][_val = _1] // Does not work?
        >> lit('}')
    ;

...

typedef Vertex3d<float32> VertexData;
typedef VertexData VertexReal3d;
typedef std::vector<VertexData> Vertices;

...

BOOST_FUSION_ADAPT_ADT(
    wc3lib::mdlx::VertexData,
    (wc3lib::float32, wc3lib::float32, obj[0], obj[0] = val)
    (wc3lib::float32, wc3lib::float32, obj[1], obj[1] = val)
    (wc3lib::float32, wc3lib::float32, obj[2], obj[2] = val)
)

...

template<typename T, typename std::size_t N>
class BasicVertex : public std::array<T, N>
{
    public:
        typedef std::array<T, N> Base;

        BasicVertex() : Base()
        {
        }

        BasicVertex(const BasicVertex<T, N> &other) : Base(other) {
    }


};


template<typename T = float32>
class Vertex3d : public BasicVertex<T, 3>
{
    public:
        typedef BasicVertex<T, 3> Base;

        Vertex3d() : Base()
        {
        }

        Vertex3d(T x, T y, T z)
        {
            (*this)[0] = x;
            (*this)[1] = y;
            (*this)[2] = z;
        }

        Vertex3d(const Base &other) : Base(other) {
        }
};
Run Code Online (Sandbox Code Playgroud)

规则顶点应返回VertexData的std :: vector.因此,重复用于解析固定数量的顶点.金额在整理列表之前的解析文件中作为整数值存储在_a中.

编译错误提示它不能在"is_sequence"和"is_container"之间有所不同.我不是灵的专家,所以我无法回答它究竟意味着什么.

seh*_*ehe 5

这是我自己制作的结果.

住在Coliru

我选择了改编VectorData类型.(我注意到你可能没有太晚了).

这使得语法变得复杂.我不确定究竟什么不起作用,因为在代码编译它确实有效的时候......

因此,让我们在代码清理和示例中进行另一个练习:

  • 删除了语义动作
  • 删除了两条规则
  • 添加了调试信息
  • 删除了','这里所需的古怪:

    '{' >> repeat(_a)[ vertex_real_3d >> (',' | &lit('}')) ] >> '}'
    
    Run Code Online (Sandbox Code Playgroud)
  • 删除了多余的用途 operator%=

  • 删除了多余的用途 qi::lit()
  • 不要qi::locals<>在语法声明中公开(这是一个实现细节)

现在代码时钟为77行(比以前减少25行):

Live On Coliru

//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

using long32  = int32_t;
using float32 = float;

namespace Geoset {
    template <typename T>
        struct Vertex3d {
            T a,b,c;
        };

    typedef Vertex3d<float32>       VertexData;
    typedef VertexData              VertexReal3d;
    typedef std::vector<VertexData> Vertices;
}

BOOST_FUSION_ADAPT_STRUCT(Geoset::VertexData, (float32, a)(float32, b)(float32, c))

template <typename Iterator, typename Skipper = qi::space_type>
struct grammar : qi::grammar<Iterator, Geoset::Vertices(), Skipper> {

    grammar() : grammar::base_type(start) {
        using namespace qi;

        vertex_real_3d = 
            '{' >> real_literal >> ','
                >> real_literal >> ','
                >> real_literal >> '}'
            ;

        vertices %=
                "Vertices"
                >> omit [ integer_literal[_a = _1] ]
                >> '{' >> repeat(_a)[ vertex_real_3d >> (',' | &lit('}')) ] >> '}'
            ;

        start = vertices;

        BOOST_SPIRIT_DEBUG_NODES((start)(vertices)(vertex_real_3d))
    }
  private:
    qi::int_parser<long32> integer_literal;
    qi::real_parser<float32> real_literal;
    qi::rule<Iterator, Geoset::VertexReal3d(), Skipper> vertex_real_3d;
    qi::rule<Iterator, Geoset::Vertices(), Skipper, qi::locals<long32> > vertices;
    qi::rule<Iterator, Geoset::Vertices(), Skipper> start;
};

int main() {
    std::string const input = "Vertices 4 { \n"
        " { 1,  2,  3  }, \n"
        " { 4,  5,  6  }, \n"
        " { 7,  8,  9  }, \n"
        " { 10, 11, 12 } \n"
    "}";

    auto f(begin(input)), l(end(input));
    grammar<std::string::const_iterator> g;

    Geoset::Vertices vertices;
    bool ok = qi::phrase_parse(f,l,g,qi::space,vertices);

    if (ok) {
        std::cout << "Parsed: " << vertices.size() << "\n";
        for (auto& v : vertices)
            std::cout << boost::fusion::as_vector(v) << "\n";
    } else
        std::cout << "Parse failed\n";

    if (f!=l)
        std::cout << "Remaining input: '" << std::string(f,l) << "'\n";
}
Run Code Online (Sandbox Code Playgroud)

  • 好吧.这是一个吃力不讨好的工作.我花时间尝试使用半熟的代码示例重现错误,您可以轻松地使其自包含.我解释说,我遗憾地无法回答你的问题,(因为你未能提供复制品).您发现需要同时将答案视为"不解释",并且在某些方面仍然抱怨我的非答案中的细节? (2认同)