保存 tuple_cat 的结果
Saving the results of a tuple_cat
我有从其他来源获取的代码。其余代码效果很好。我正在尝试使用以下代码附加到元组:
// 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 语义类似于 push_back 的 std::vector 等
有什么建议吗?
PushFront::operator()
returns 新元组,附加项后跟给定元组中的项。它不会附加到传入的那个。
不可能推入现有的,因为 tuple
个 N+1
个元素与 tuple
个 N
个元素是不同的 C++ 类型。
我有从其他来源获取的代码。其余代码效果很好。我正在尝试使用以下代码附加到元组:
// 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 语义类似于 push_back 的 std::vector 等
有什么建议吗?
PushFront::operator()
returns 新元组,附加项后跟给定元组中的项。它不会附加到传入的那个。
不可能推入现有的,因为 tuple
个 N+1
个元素与 tuple
个 N
个元素是不同的 C++ 类型。