让我们A:
struct A {
int a;
std::string b;
struct keys {
struct a;
struct b;
};
};
Run Code Online (Sandbox Code Playgroud)
我想fusion::map从结构中生成一个它包含fusion::pairs:fusion::pair<A::keys::a, int>和fusion::pair<A::keys::b, std::string>.就像是
A a;
fusion::make_map<A>(a)
Run Code Online (Sandbox Code Playgroud)
我试过了 BOOST_FUSION_ADAPT_ASSOC_STRUCT
BOOST_FUSION_ADAPT_ASSOC_STRUCT(
A,
(int, a, A::keys::a)
(std::string, b, A::keys::b)
Run Code Online (Sandbox Code Playgroud)
)
这使A适合用作关联序列,但我还没有找到从中构造地图的方法.特别是,如果我迭代它,我只得到值.如果我可以迭代那些真正有用的键,因为那时我可以压缩值和键来构建一个映射,但我还没有找到一种方法来做到这一点.
我正在寻找一种方法来创建一个Boost.Fusion序列包装器,它本身就是一个Fusion序列,并将所有'调用'转发到它的包装序列.有些东西
template< typename Sequence >
struct sequence_wrapper
{
explicit sequence_wrapper( Sequence const& s ) : seq( s ){}
Sequence seq;
};
Run Code Online (Sandbox Code Playgroud)
这里sequence_wrapper< Sequence >是一个融合序列以及和作品一样Sequence会.我需要这个的原因是我有几个函数在Fusion序列上运行(所有元素都满足一些特殊要求),我想添加一些语法糖,我需要一个自定义类型来添加重载运算符.我不需要sequence_wrapper上的操作结果来返回sequence_wrapper,只有语法糖相关的调用才会返回(手动)包装的序列.例如,使用逗号运算符将元素附加到序列(有点像Boost.Assign for Fusion序列):
template< typename Sequence, typename T >
sequence_wrapper<
typename boost::fusion::result_of::push_back<
Sequence const&
, T
>::type
> operator ,( Sequence const& seq, T const& v )
{
return
sequence_wrapper<
typename boost::fusion::result_of::push_back<
Sequence const&
, T
>::type
>( boost::fusion::push_back( seq, v ) ) …Run Code Online (Sandbox Code Playgroud) 我还在评估我是否应该开始使用D来进行物理数据代码的原型设计.
阻止我的一件事是我喜欢提升,特别是融合和mpl.
D对于模板元编程来说是惊人的,我认为它可以做mpl和融合的东西,但我想确定.
即使我开始使用d,我也需要一段时间才能达到mpl水平.所以我希望有人分享他们的经验.
(通过mpl我的意思是使用stl作为模板和融合,我的意思是stl for tuples.)
关于性能的说明也会很好,因为它在物理模拟中是至关重要的.
我正在寻找实现variadic函数的最简单方法,它采用boost :: spirit :: qi规则列表并将列表扩展为格式表达式:rule1 | rule2 | rule3 | ....让我们假设规则不合成任何属性.非常感谢您的帮助.
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <iostream>
#include <boost/spirit/include/phoenix_operator.hpp>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::ascii::space;
using boost::spirit::iso8859_1::char_;
typedef qi::rule<std::string::const_iterator,ascii::space_type> mrule_t;
typedef qi::rule< std::string::const_iterator,std::string() > wrule_t;
Run Code Online (Sandbox Code Playgroud)
//How to deduce expandBitwise() return type ?
template<typename T>
T expandBitwise(T& t)
{
return t.rule_;
}
template<typename T,typename ...Tail>
T expandBitwise(T& t,Tail& ...tail)
{
return t.rule_ | expandBitwise(tail...);
}
struct TStruct
{
mrule_t …Run Code Online (Sandbox Code Playgroud) 如何识别boost :: fusion向量中的类型?
例如
fusion::vector<int, double, string> v;
Run Code Online (Sandbox Code Playgroud)
那么一些让我认定v[0]为类型int,v[1]类型double和v[2]类型的东西string.
谢谢.
例如,假设我有以下结构/子结构定义:
struct address_rec
{
std::string m_street;
std::string m_state;
unsigned m_zip;
};
struct employee_rec
{
std::string m_name;
address_rec m_address;
};
Run Code Online (Sandbox Code Playgroud)
我应该如何使用BOOST_FUSION_ADAPT_STRUCT的employee_rec?
我为标识符定义了一个规则:以字母字符开头,后跟任意数量的字母数字字符.当我直接解析为std::string包含单个的自适应结构时,我有不同的结果std::string.
如果我的语法属性是std::string,Qi将正确地调整字符序列.但是使用结构,只存储第一个字符.我不太清楚为什么会这样.(注意,如果结构"真正"适应,或者它是由Fusion内联定义的,则没有区别.)
这是一个SSCCE,可配置为调试:
// Options:
//#define DEFINE_STRUCT_INLINE
//#define DEBUG_RULE
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted/struct/define_struct_inline.hpp>
#include <boost/fusion/include/define_struct_inline.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
#ifdef DEFINE_STRUCT_INLINE
namespace example
{
BOOST_FUSION_DEFINE_STRUCT_INLINE(
identifier_result,
(std::string, name)
)
}
#else
namespace example
{
struct identifier_result
{
std::string name;
};
}
BOOST_FUSION_ADAPT_STRUCT(
example::identifier_result,
(std::string, name)
)
#endif
namespace example
{
typedef std::string identifier_result_str;
template <typename Iterator, typename Result>
struct identifier_parser …Run Code Online (Sandbox Code Playgroud) 我试图了解使用Fusion的重点,并且我对这个简单的例子感到难过:
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/transform.hpp>
template< typename T >
struct S {
typedef T type;
};
struct S_f {
template< typename T >
struct result {
typedef typename T::type type;
};
};
int main () {
using namespace boost;
typedef fusion::vector<S<int>> from_type;
BOOST_MPL_ASSERT((fusion::traits::is_sequence< fusion::vector< int > > ));
typedef fusion::result_of::transform< from_type, S_f >::type to_type;
BOOST_MPL_ASSERT((fusion::traits::is_sequence< to_type > ));
typedef fusion::result_of::as_vector< to_type >::type value_type; // error
}
Run Code Online (Sandbox Code Playgroud)
断言通过,但value_type的typedef失败,错误如下.我无法为代码和文档之间的任何差异提供资金,也无法在stackoverflow或boost邮件列表的其他地方补救.
AFAICT代码是正确的:应用变换元函数的结果是transform_view,transform_view是一个序列,如传递断言所示.然而,as_vector元函数在transform_view上的应用失败了.是什么赋予了?!
任何帮助表示赞赏.我对混合mpl不感兴趣.我知道我可以通过MPL和一些关于类型操作的融合问题绕道而行,这些问题有关于类型操作的答案,主张MPL.根据文档,我不需要MPL.
clang++ -std=c++1z -c t.cpp
In file included …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Boost.Sprit x3将两个整数的序列匹配成一个std::pair<int, int>.根据文档判断,以下代码应该编译:
#include <string>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
int main()
{
using namespace boost::spirit::x3;
std::string input("1 2");
std::pair<int, int> result;
parse(input.begin(), input.end(), int_ >> int_, result);
}
Run Code Online (Sandbox Code Playgroud)
但是,它只匹配第一个整数.如果我改变std::pair<int, int> result;对int result;,然后打印result,我得到1我的输出.
为什么会这样?这不是int_ >> int_定义匹配(并设置为属性)两个整数的解析器的正确方法吗?
给定这样的结构:
struct Foo
{
int x;
int y;
double z;
};
BOOST_FUSION_ADAPT_STRUCT(Foo, x, y, z);
Run Code Online (Sandbox Code Playgroud)
我想生成一个这样的字符串:
"{ int x; int y; double z; }"
Run Code Online (Sandbox Code Playgroud)
我已经看到了如何打印 Fusion适应结构的值,但是在这里我只需要打印类型和名称。
我该如何简单地做到这一点?如果有更好的方法,我不嫁给Boost.Fusion。
boost-fusion ×10
c++ ×8
boost ×3
boost-spirit ×3
boost-mpl ×1
c++11 ×1
c++14 ×1
d ×1
parsing ×1
stl ×1