我试图在我的业力语法中访问一个指针向量,但收效甚微。指针类型是不可复制的,因此使用它的规则必须引用:
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace karma = boost::spirit::karma;
namespace fusion = boost::fusion;
namespace phx = boost::phoenix;
struct test1 : boost::noncopyable {
test1(int i = 0) : value(i) {}
int value;
};
struct test2 : boost::noncopyable {
int value;
std::vector<test1*> vector;
};
BOOST_FUSION_ADAPT_STRUCT( test1, (int, value) );
BOOST_FUSION_ADAPT_STRUCT( test2, (int, value) (std::vector<test1*>, vector) );
typedef std::ostream_iterator<char> Iterator;
int main() {
karma::rule<Iterator, test1*()> t1r;
karma::rule<Iterator, test2&()> t2r;
t2r %= "test 2 rule:" << karma::int_ << karma::eol << (t1r % karma::eol);
t1r %= "test 1 rule: " << karma::int_;
std::stringstream stream;
std::ostream_iterator<char> out(stream);
test2 t;
t.vector.push_back(new test1(2));
t.vector.push_back(new test1(3));
t.vector.push_back(new test1(4));
t.vector.push_back(new test1(5));
t.value = 1;
karma::generate(out, t2r, t);
std::cout<<stream.str()<<std::endl;
}
这编译但返回:测试 2 规则:1,测试 1 规则:25104656,测试 1 规则:25104720等等。我知道在这种简单的情况下我可以做到
t1r = "test 1 rule: " << karma::int_[karma::_1 = phx::bind(&test1::value, *karma::_val)];
来解决它,但实际上value是另一种不可复制的类型,它应该被传递给语法,因此我需要使用示例中所做的结构调整。
我也知道这里提到的自定义点 deref_iterator ,但是,我在模板库上工作,并且认为不可能将 deref_iterator 专门化为依赖于模板的类型。
关于如何使示例工作的任何想法?