我有从另一个来源获取的代码。其余的代码效果很好。我正在尝试使用以下代码附加到元组:
// 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。
有什么建议么?