小编Giu*_*ano的帖子

spirit :: qi:将继承的属性引用传递给phoenix :: function

以下代码是我正在尝试做的简化版本.基本上,我有一个容器数据成员struct(玩具代码中的int_holder).我想插入一个对象(在本例中为int)并返回父qi :: rule指向新插入对象的指针.

我通过引用将int_holder传递给语法,以便在解析时用值填充它,因此int_holder将是语法的继承属性.码:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <vector>

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

struct int_holder
{
    int_holder() {}
    std::vector<int> ints;
};


struct insert_impl
{
    template<class, class>
    struct result { typedef int* type; };
    int* operator() (int_holder& holder, int i) const
    {
        holder.ints.push_back (i);
        return &holder.ints.back();
    }
};

template<class Iterator>
struct my_grammar : qi::grammar<Iterator, int*(int_holder&)>
{
    my_grammar () : my_grammar::base_type (start)
          , insert (insert_impl())
    {
        using qi::_val;
        using qi::int_;
        using qi::_r1;
        using qi::_1;

        using phoenix::ref;

        start …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit-qi

6
推荐指数
1
解决办法
1143
查看次数

调试C++程序时出现奇怪的gdb消息

我使用apple的gdb,版本如下

GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul  3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".
Run Code Online (Sandbox Code Playgroud)

我不使用Xcode IDE因为我需要makefile管理的项目更方便,特别是因为我经常需要在远程机器上编译和运行程序.我最近开始使用下面的C++ 11个功能(在G ++ - 4.6和4.7的MacPorts从):移动构造,nullptr,汽车和decltype.我经常需要d3bug我的代码,并出现以下奇怪的消息,而不是踩到一个函数:

Die: DW_TAG_unspecified_type (abbrev = …
Run Code Online (Sandbox Code Playgroud)

c++ gdb osx-snow-leopard c++11

5
推荐指数
2
解决办法
2033
查看次数

在编译时将mpl :: vector_c复制到静态数组

使用C++ 11我有类似的东西

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/size.hpp>

#include <boost/array.hpp>

#include <iostream>

namespace mpl = boost::mpl;

template<std::size_t ... Args>
struct Test
{
            typedef mpl::vector_c<std::size_t, Args ...> values_type;

            static const boost::array<std::size_t, sizeof...(Args)> values;
};


int main (int argc, char** argv)
{
            Test<3,2,5,6,7> test;
            return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想用mpl :: vector_c中的值'contains'初始化boost :: array内容.此初始化应在编译时执行.我已经在SO上看到了一些使用预处理器的解决方案,但我不知道如何将它们应用于可变参数模板的情况.

请注意,在上面的示例代码中,mpl :: vector_c的元素与Test的模板参数相同.在实际代码中并非如此,而是values_type具有length ==模板参数的数量,但实际值是由一系列mpl算法的应用产生的.因此,不要假设参数是相同的.

希望问题很清楚,谢谢!

c++ templates boost boost-mpl c++11

5
推荐指数
1
解决办法
1112
查看次数

boost.proto +修改表达式树到位

背景问题:boost.proto +在构建表达式树之前检测无效终端.

嗨,我想要实现的是

  1. 创建一个表达式树的副本,其中所有向量都用它们的开始迭代器代替(在我的例子中是一个原始指针)
  2. 将迭代器增加到位
  3. 在树中取消引用迭代器,但该部分应该相对容易.

所以,对于1.我最终得到了这个代码

///////////////////////////////////////////////////////////////////////////////
// A transform that converts all vectors nodes in a tree to iterator nodes
struct vector_begin : proto::transform <vector_begin>
{
    template<typename Expr, typename Unused1, typename Unused2>
    struct impl : boost::proto::transform_impl<Expr, Unused1, Unused2>
    {
        // must strip away the reference qualifier (&)
        typedef typename proto::result_of::value<
                typename boost::remove_reference<Expr>::type
            >::type vector_type;

        typedef typename proto::result_of::as_expr
            <typename vector_type::const_iterator>::type result_type;

        result_type operator ()(
              typename impl::expr_param var
            , typename impl::state_param
            , typename impl::data_param) const
        {
            typename vector_type::const_iterator …
Run Code Online (Sandbox Code Playgroud)

c++ boost template-meta-programming boost-proto

5
推荐指数
1
解决办法
482
查看次数

Boost.MPL转换状态?

我有以下mpl序列

boost::mpl::vector_c<std::size_t, 0, 1, 2, 0, 1, 0>
Run Code Online (Sandbox Code Playgroud)

我需要根据以下算法(运行时版本)对其进行转换:

i=0
output_sequence=[]
for k in (0,...,len(input_sequence)-1):
if input_sequence[k] == 0:
    output_sequence.append(i)
    i=i+1
else:
    output_sequence.append(-1)
Run Code Online (Sandbox Code Playgroud)

我的结果应该是:

boost::mpl::vector_c<std::size_t, 0, -1, -1, 1, -1, 2>
Run Code Online (Sandbox Code Playgroud)

我可以想象至少有两种方法可以在运行时使用std :: transform或std :: accumulate来实现,但是不知道如何在编译时使用mpl实现相同的结果.对我来说,主要的问题是以某种方式存储状态'i'(当前发现的零数)以及输出序列.

非常感谢你!

添加

基于HighCommander4的回答,我想与大家分享所采用的方法.实际上,可以使用boost::mpl::fold自定义元函数.与HighCommander4相比,唯一的概念变化是将当前可用的自由索引打包为增长输出序列的第一个元素.它可以在类型计算结束时使用删除boost::mpl::pop_font.

#include <boost/mpl/size_t.hpp>
#include <boost/mpl/equal_to.hpp>

#include <boost/mpl/fold.hpp>

#include <boost/mpl/front.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/pop_front.hpp>

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/at.hpp>

#include <boost/mpl/print.hpp>

namespace mpl=boost::mpl;

struct assign_index
{
                enum { dim_dynamic, dim_static };
                template<typename Output, typename Index,
                         typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates boost-mpl

2
推荐指数
1
解决办法
949
查看次数