C++ MPL更换,无需铸造熔合容器

C++ MPL更换,无需铸造熔合容器,c++,boost,c++14,boost-mpl,boost-fusion,C++,Boost,C++14,Boost Mpl,Boost Fusion,我有这门课 struct MyChildrenNeedsSpace : HaveChildren<MyChildrenNeedsSpace> { typedef childrenListType<string, string, string, string> context; const context children; MyChildrenNeedsSpace() : children("this", "sentence", "need

我有这门课

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;
    }
};
强制转换我的容器类型-您能帮助使一个可以在编译时定义类型的函数成为以下类吗

struct childrenListType : public  boost::fusion::vector<CHILDREN_TYPES...>

对于C++14,我看不出这里需要boost,或者我错过了您的一个需求?以下内容将使用普通C++14在类型之间添加空格:

struct space {};
template<typename, typename=void> struct spacer;

template<template<typename...> class T>
struct spacer<T<>> { using type = T<>; };

template<template<typename...> class T,typename T1, typename... Ts>
struct spacer<T<T1,Ts...>>
    : spacer<T<T1,Ts...>, std::make_index_sequence<2*sizeof...(Ts)+1>> {};

template<template<typename...> class T,typename... Ts, std::size_t... Ns>
struct spacer<T<Ts...>, std::index_sequence<Ns...>>
{
    using tuple = std::tuple<Ts...>;
    using type =
        T<std::conditional_t<(Ns%2)==0,std::tuple_element_t<Ns/2,tuple>,space>...>;
};

template<typename T> using spacer_t = typename spacer<T>::type;
struct space{};
模板结构垫片;
模板
结构分隔符{using type=T;};
模板
结构垫片
:间隔{};
模板
结构垫片
{
使用tuple=std::tuple;
使用类型=
T
};
使用间隔符的模板\u t=typename间隔符::type;


以上内容保留了外部容器类型,因此在您的情况下,如果传入一个
boost::fusion::vector
,这也是您将得到的结果(只需额外的
空间即可)。

不要得到它,如果你知道每个元素都必须用一个空格隔开,为什么不在你把所有东西都流出来的地方加上空格呢?另外,如果你不能处理不同大小的元组,你的流媒体功能也会被破坏?我在这里粘贴的代码只是为了展示我想要解决的概念。这并不仅仅是为了让它输出正确的东西-我正在开始学习MPL,在这里被绊住了,所以我希望有人会这样说“哦,你只需要使用boost::XXXXX就可以了”或者告诉我正确的方向。使用C++14,我看不出有必要在这里使用boost-或者我缺少了你的一个需求?@Hansveselgård嗯…如果我遗漏了一个需求,那么请为我详细说明,不要重复我所说的。谢谢。”使用C++14,我看不出有必要在这里提高——或者我错过了你的一个要求?“哦,这只是一个更大的代码库中的一些片段,我编写了一些存根/伪代码,以便人们在寻求帮助时更容易理解这个问题。我是TMP的新手,所以我不知道所有正确的单词,所以我更喜欢展示我想做的事情。“如果我错过了一个要求,那么请给我解释清楚,不要重复我说的话。谢谢”你完全理解我。非常感谢你的回答-我很惊讶。我需要一些时间来真正理解代码。我正在观看Walter E.Brown的《现代模板元编程:概要》,并通过大量阅读来更好地理解现代模板元编程。我需要理解很多概念,而不是很多最新东西的学习工具(比如make_index_序列)。我目前的策略是使用参数包或类型列表作为容器实现以下algo作为模板函数,但您的was要聪明得多,@Hansveselgård啊,好的。好吧,那样的话,我很高兴能帮上忙。根据我的经验,在没有外部库(当然包括标准库)的帮助下,了解可变模板、TMP等是值得的。在您能够充分利用更复杂的库(如MPL等)之前,您需要自己了解基本知识,而这些库通常甚至不需要,因此可以解决您在现实生活中遇到的大多数问题。
<string,string,string>
<string,space,string,space,string>
typedef boost::mpl::replace< context, string, stringAndSpace >::type replacedContext;
struct childrenListType : public  boost::fusion::vector<CHILDREN_TYPES...>
(this  sentence  needs  spaces )
struct space {};
template<typename, typename=void> struct spacer;

template<template<typename...> class T>
struct spacer<T<>> { using type = T<>; };

template<template<typename...> class T,typename T1, typename... Ts>
struct spacer<T<T1,Ts...>>
    : spacer<T<T1,Ts...>, std::make_index_sequence<2*sizeof...(Ts)+1>> {};

template<template<typename...> class T,typename... Ts, std::size_t... Ns>
struct spacer<T<Ts...>, std::index_sequence<Ns...>>
{
    using tuple = std::tuple<Ts...>;
    using type =
        T<std::conditional_t<(Ns%2)==0,std::tuple_element_t<Ns/2,tuple>,space>...>;
};

template<typename T> using spacer_t = typename spacer<T>::type;