【发布时间】:2015-06-06 17:52:12
【问题描述】:
我有这门课
struct MyChildrenNeedsSpace : HaveChildren<MyChildrenNeedsSpace>
{
typedef childrenListType<string, string, string, string> context;
const context children;
MyChildrenNeedsSpace() : children("this", "sentence", "needs", "spaces")
{
std::cout << endl << "The children type is:" << endl << typeid(children).name() << endl;
}
};
它使用 CRTP 允许 HaveChildren 类访问其子成员变量。
childrenListType 是一个继承自 boost::fusion::vector 的类。
我想以编程方式使 children 成员变量在每个字符串之间包含一个空格类。
所以如果我输入:
<string,string>
孩子变成:
<string, space,string>
如果我输入:
<string,string,string>
变成了
<string,space,string,space,string>
等等
我正在使用 boost fusion,所以它必须在编译时。
我尝试过这样做:
struct MyChildrenNeedsSpaceWithReplacer : HaveChildren<MyChildrenNeedsSpaceWithReplacer>
{
typedef childrenListType<string, string, string, string> context;
typedef boost::mpl::replace< context, string, stringAndSpace >::type replacedContext;
const replacedContext children;
MyChildrenNeedsSpaceWithReplacer() : children( "this" ,"sentence" , "needs" , "spaces")
{
std::cout << endl << "The children type is:" << endl <<typeid(children).name() << endl;
}
};
然后 MPL:replace 将容器的类型从我自己的类(继承自 boost::fusion::vector)更改为 boost::fusion::vector4,这会破坏我的流式重载。
您可能会注意到,我将每个字符串替换为 stringAndSpace,而不是 <string,space>。
拥有<string,space> 是最好的——但另一种方式对我来说更容易。
总结一下:
typedef boost::mpl::replace< context, string, stringAndSpace >::type replacedContext;
转换我的容器类型 - 你能帮助制作一个可以在编译时将类型定义为以下类的函数
struct childrenListType : public boost::fusion::vector<CHILDREN_TYPES...>
在我输入的每个字符串之间使用一个空格的模板参数?
我已将完整来源发布在:http://ideone.com/XxYTOt
他们的编译器 typeinfo 说没有 mpl 替换子类型是:16childrenListTypeIISsSsSsSsEE
并与:N5boost6fusion7vector4I14stringAndSpaceS2_S2_S2_EE
您还可以看到流重载失败,因为它输出括号:
(this sentence needs spaces )
【问题讨论】:
-
不明白,如果你知道每个元素都必须用空格隔开,那为什么不直接在输出所有内容的地方添加空格?另外我想如果你不能处理不同大小的 元组,你的流媒体功能就会被破坏?
-
我这里贴的代码只是为了展示我要解决的概念。
-
这不仅仅是让它输出正确的东西——我开始学习 MPL 并被困在这里,所以我希望有人可能会说“哦,你只需要使用 boost ::XXXXX 代替”或告诉我正确的方向。
标签: c++ boost c++14 boost-mpl boost-fusion