保存tuple_cat的结果

时间:2015-03-05 19:13:54

标签: c++ templates c++11 variadic-templates tuple-packing

我有来自其他来源的代码。其余的代码效果很好。我试图使用以下代码附加到元组:

// Because std::make_tuple can't be passed
// to higher order functions.
constexpr struct MakeTuple
{
    template< class ...X >
    constexpr std::tuple<X...> operator () ( X ...x ) {
        return std::tuple<X...>( std::move(x)... );
    }
} tuple{};

constexpr struct PushFront
{
    template< class ...X, class Y >
    constexpr auto operator () ( std::tuple<X...> t, Y y )
        -> std::tuple< Y, X... >
    {
        return std::tuple_cat( tuple(std::move(y)), std::move(t) );
    }
} pushFront{};

template <template <typename...> class T, typename... Args, typename... Obs>
T<Obs...> MakeSubject(std::tuple<Obs ...> &&obs, Args&& ... args)
{
    return T<Obs...>(std::move(obs), args...);
}

template <template <typename...> class T, typename... Args, typename... Obs>
std::tuple<T<Obs...>> Store(std::tuple<Obs ...> &&obs, Args&& ... args)
{
    return std::make_tuple(T<Obs...>(std::move(obs), args...));
}

template <typename Base> class Observer
{
}

class Printer : public Observer<Printer>
{
}

template <typename T, typename... Obs> class Subject
{
private:
    std::tuple<Obs &...> observers;
}

template <typename... Obs>
class Pressure : public Subject<Pressure<Obs...>, Obs...> 
{
}

std::ostream& operator << (std::ostream& out, const Printer& ac)
{
    //stuff

    return out;
}

我在这样的循环之外有代码:

const Printer sentinel; 
auto store = Store<Pressure>(std::move(std::tuple<Printer>(sentinel)), fakePressure); // The first one is just a delimiter

问题是在循环中,当我尝试通过说:

追加存储元组
while(true) // A Demo loop
{
    auto subject = MakeSubject<Pressure>(std::move(obs), q);
    pushFront(store, subject.Observers()));
    std::cout << store; // Always empty
    std::cout << pushFront(store, subject.Observers()); // This works and shows whatever I passed in, but the list of tuples doesn't grow from previous appends.
}
通过附加越来越多的std :: tuples

商店不会增长。我希望pushFront for tuples的语义类似于st_ :: vector等的push_back。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

PushFront::operator()返回带有附加项的新元组,后跟给定元组中的项。它不附加到传入的那个。

无法推入现有元素,因为tupleN+1元素与tupleN元素的C ++类型不同。