0

我有从另一个来源获取的代码。其余的代码效果很好。我正在尝试使用以下代码附加到元组:

// 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.
}

store 不会通过添加越来越多的 std::tuples 来增长。我希望元组的 pushFront 语义类似于 std::vector 等的 push_back。

有什么建议么?

4

1 回答 1

1

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

不可能推入现有的,因为 a tupleof元素是与 a ofN+1元素不同的 C++ 类型。tupleN

于 2015-03-05T20:17:08.910 回答