【问题标题】:compiling boost.spirit.karma example, customize_embedded_container.cpp fails编译 boost.spirit.karma 示例,customize_embedded_container.cpp 失败
【发布时间】:2019-07-26 01:23:43
【问题描述】:

使用 gcc 或 clang 最新,boost 1.68(或 1.69),

编译 boost.spirit.karma 示例 customize_embedded_container.cpp 失败,除非在第 26 行将 type 替换为 value_type。

另一个有用的例子,customize_use_as_container.cpp也失败了,在indirect_iterator中询问value_type等, "在'std::iterator_traits'中没有命名类型'value_type'"

供参考,示例:https://www.boost.org/doc/libs/1_69_0/libs/spirit/example/karma/customize_use_as_container.cpp

  • 有没有关于在 boost.spirit.karma 中编译的容器专业化的线索或工作示例?

【问题讨论】:

  • 欢迎来到 Stack Overflow!我们愿意帮助你。请花时间阅读tour 并阅读How to Ask,然后阅读edit 相应的问题。请创建a Minimal, Complete, and Verifiable example,并提供示例输入、输出和您收到的错误消息(如果有)。这将帮助我们确定正在发生的事情并提高您获得答案的机会。

标签: c++ boost


【解决方案1】:

鉴于我的情况,解决方案的框架,或者更确切地说是一种解决方法,只是使用 boost.spirit 中真正容易工作的部分:

namespace X {
  struct p { double t,v;};// point, time, value
  struct ts { // simplified time-series representation, time-axis computed
     bool s;
     double t0,dt;size_t n;// ti = t0 + dt*i
     std::vector<double> v;// v.size()===n
     p point(size_t i ) const  { return p{t0+dt*i,v[i]};}
  };

  //--would like to output grammar like {pfx:(true|false),data:[[t,v]..]}

  //--; first construct a container class out of ts, using boost iterator_facade 
  //--; to get away with few lines.(simplified..)
  struct ts_c {
     using value_type=p;// iterate over (t,v), t is computed, v is plain vector
     ts * c;
     struct iterator:public::boost::iterator_facade<iterator,p,boost::random_access_traversal..> { //..
       iterator(ts*c,size_t pos=0u):c{c},pos{pos} {}
        //.. plain vanilla impl. goes here according to boost::iterator_facade.
       private:
         ts* c;
         size_t pos;
     };
     using const_iterator=iterator;
     iterator begin() const;//.. etc. impl. of need it stuff.
  };
}
// use FUSION to adapt the ts-type, let last tuple member return the
// tuple members:
// 0 true if n>0, otherwise false
// 1 true if ts.s is true
// 2 returns a vanilla iterable container, ts_c exposing type p
BOOST_FUSION_ADAPT_ADT(
    X::ts,
    (bool,bool, obj.n>0,/**/)
    (bool,bool,obj.s,/**/)
    (X::ts_c,X::ts_c, X::ts_c(&obj),/**/)
)
//-- then combining the boost spirit complex example to emit the point class p
namespace X {
    namespace ka=boost::spirit::karma;

    /** generate a point like [123.0, 3.14], or [123.0,null] if !isfinite. 
    *  using the complex example pattern given in boost::spirit,
    */
    template<class OutputIterator>
    struct point_generator:ka::grammar<OutputIterator,p()> {

        point_generator():point_generator::base_type(pg) {
            using ka::true_;
            using ka::bool_;
            using ka::omit;
            pg = 
                &true_ <<( '[' <<  double_<< ',' << double_ << ']')
                    |
                      omit[bool_]<<   ( '[' <<  double_<< ',' << "null" << ']')

            ;
        }
        ka::rule<OutputIterator,p()> pg;
    };

    /** @brief a ts generator
    *
    * outputs:
    *   {pfx:(true|false),data:[[t,v],..]}
    * or if empty
    *   {pfx:null,data:null}
    */
    template<class OutputIterator>
    struct ts_generator:ka::grammar<OutputIterator,ts()> {

        ts_generator():ts_generator::base_type(tsg) {
            using ka::true_;
            using ka::bool_;
            using ka::omit;
            tsg =
                &true_ <<"{pfx:"<<bool_<<",data:"<<( '[' << pt_ % ',' << ']')<<'}'
                    |
                      omit[bool_]<<  "{pfx:null: data:null}"

            ;
        }
        ka::rule<OutputIterator,ts()> tsg;
        point_generator<OutputIterator> pt_;
    };

}

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2022-10-04
    • 1970-01-01
    • 2021-10-07
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多