通过通过另一个std的类型获取std ::元组的元素
我的结构使管理元组变得更容易:
template<class... Ts>
struct Store
{
using base_type = std::tuple<Ts...>;
base_type store;
Store(Ts... args)
{
store = {args...};
}
template<size_t... Ints>
decltype(auto) Get()
{
if constexpr(sizeof...(Ints) == 1)
return std::get<Ints...>(store);
else
return std::make_tuple(std::get<Ints>(store)...);
}
template<class... Us>
decltype(auto) Get()
{
if constexpr(sizeof...(Us) == 1)
return std::get<Us...>(store);
else
return std::make_tuple(std::get<Us>(store)...);
}
};
我还具有使我可以组合模板参数的结构:
template <typename... Ts>
struct store_helper
{ using type = Store<Ts...>; };
template <typename... Ts>
struct store_helper<Store<Ts...>>
{ using type = Store<Ts...>; };
template<typename ... Ts1, typename... Ts2>
struct store_helper<Store<Ts1...>, Ts2...>
: public store_helper<Store<Ts1..., Ts2...>>
{ };
template<typename ... Ts1, typename ... Ts2, typename... Ts3>
struct store_helper<Store<Ts1...>, Store<Ts2...>, Ts3...>
: public store_helper<Store<Ts1..., Ts2...>, Ts3...>
{ };
template<typename... Ts>
using Scheme = typename store_helper<Ts...>::type;
现在我可以做到这一点:
int main() {
using Vector2D = Scheme<int, int>;
using Position = Scheme<Vector2D>;
using Direction = Scheme<Vector2D>;
using Player = Scheme<Position, Direction, float>;
Player store(33, 44, 12, 122, 4.0);
auto float_num = store.Get<float>();
std::cout << float_num << std::endl;
return 0;
}
但是我也想这样做:
auto [x, y] = store.Get<Direction>();
std::cout << x << std::endl;
std::cout << y << std::endl;
我需要通过通过元组来检索数据他创造了它。有什么方法可以实施此语法?
I have a structure that makes it easier to manage the tuple:
template<class... Ts>
struct Store
{
using base_type = std::tuple<Ts...>;
base_type store;
Store(Ts... args)
{
store = {args...};
}
template<size_t... Ints>
decltype(auto) Get()
{
if constexpr(sizeof...(Ints) == 1)
return std::get<Ints...>(store);
else
return std::make_tuple(std::get<Ints>(store)...);
}
template<class... Us>
decltype(auto) Get()
{
if constexpr(sizeof...(Us) == 1)
return std::get<Us...>(store);
else
return std::make_tuple(std::get<Us>(store)...);
}
};
I also have structures that allow me to combine template arguments:
template <typename... Ts>
struct store_helper
{ using type = Store<Ts...>; };
template <typename... Ts>
struct store_helper<Store<Ts...>>
{ using type = Store<Ts...>; };
template<typename ... Ts1, typename... Ts2>
struct store_helper<Store<Ts1...>, Ts2...>
: public store_helper<Store<Ts1..., Ts2...>>
{ };
template<typename ... Ts1, typename ... Ts2, typename... Ts3>
struct store_helper<Store<Ts1...>, Store<Ts2...>, Ts3...>
: public store_helper<Store<Ts1..., Ts2...>, Ts3...>
{ };
template<typename... Ts>
using Scheme = typename store_helper<Ts...>::type;
Now I can do this:
int main() {
using Vector2D = Scheme<int, int>;
using Position = Scheme<Vector2D>;
using Direction = Scheme<Vector2D>;
using Player = Scheme<Position, Direction, float>;
Player store(33, 44, 12, 122, 4.0);
auto float_num = store.Get<float>();
std::cout << float_num << std::endl;
return 0;
}
But I'd also like to do that:
auto [x, y] = store.Get<Direction>();
std::cout << x << std::endl;
std::cout << y << std::endl;
I need to retrieve data by passing the tuple from which it he created. Is there any way to implement this syntax ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论