结构数据成员的decltype,使用结构化绑定
Decltype of struct data members, using structured binding
我有一个模板,它采用在结构上可绑定到两个别名的类型(可以是元组,也可以是结构)。我需要别名指向的那两个变量的类型。
template <typename T>
T obj;
// type of a/b in auto&& [a, b] = obj ?
在实际使用结构化绑定之前我需要知道类型:
template <typename S>
void fn(ranges::any_view<S> range_of_tuple_or_struct_or_pair) {
last_from = std::numeric_limits<?????>::max();
// ????? should be the type of from (or to, they should be the same types) of:
// auto&& [from, to] = <range element>
for (auto&& [from, to] : range_of_tuple_or_struct_or_pair) {
if (from != last_from) {
...;
last_from = from;
}
}
}
也许您可以在 last_from
声明之前添加一个额外的结构化绑定并从中获取 from
类型。即
const auto& [first_from, first_to] = *range_of_tuple_or_struct_or_pair.begin();
last_from = std::numeric_limits<std::decay_t<decltype(first_from)>>::max();
不是很酷,但也许有用
我想你可以这样做:
#include <type_traits>
template <typename T, typename U>
struct Identity {
using type_first = T;
using type_second = U;
};
template <typename T>
constexpr auto GetTypes(const T& obj) noexcept {
const auto& [x, y] = obj;
return Identity<std::remove_cv_t<decltype(x)>,
std::remove_cv_t<decltype(y)>>{};
}
template <typename T>
void foo(T obj) {
// T is structurally-bindable with two fields
constexpr auto Types = GetTypes(obj);
using t1 = typename decltype(Types)::type_first;
using t2 = typename decltype(Types)::type_second;
// t1 is the first type
// t2 is the second type
}
请注意,constexpr
没有开销,即使没有启用优化。
我有一个模板,它采用在结构上可绑定到两个别名的类型(可以是元组,也可以是结构)。我需要别名指向的那两个变量的类型。
template <typename T>
T obj;
// type of a/b in auto&& [a, b] = obj ?
在实际使用结构化绑定之前我需要知道类型:
template <typename S>
void fn(ranges::any_view<S> range_of_tuple_or_struct_or_pair) {
last_from = std::numeric_limits<?????>::max();
// ????? should be the type of from (or to, they should be the same types) of:
// auto&& [from, to] = <range element>
for (auto&& [from, to] : range_of_tuple_or_struct_or_pair) {
if (from != last_from) {
...;
last_from = from;
}
}
}
也许您可以在 last_from
声明之前添加一个额外的结构化绑定并从中获取 from
类型。即
const auto& [first_from, first_to] = *range_of_tuple_or_struct_or_pair.begin();
last_from = std::numeric_limits<std::decay_t<decltype(first_from)>>::max();
不是很酷,但也许有用
我想你可以这样做:
#include <type_traits>
template <typename T, typename U>
struct Identity {
using type_first = T;
using type_second = U;
};
template <typename T>
constexpr auto GetTypes(const T& obj) noexcept {
const auto& [x, y] = obj;
return Identity<std::remove_cv_t<decltype(x)>,
std::remove_cv_t<decltype(y)>>{};
}
template <typename T>
void foo(T obj) {
// T is structurally-bindable with two fields
constexpr auto Types = GetTypes(obj);
using t1 = typename decltype(Types)::type_first;
using t2 = typename decltype(Types)::type_second;
// t1 is the first type
// t2 is the second type
}
请注意,constexpr
没有开销,即使没有启用优化。