鉴于这种类型:
template<Ratio r, Symbol s>
struct base_unit {
using ratio = r;
using symbol = s;
};
template <BaseUnit... baseUnits>
struct derived_unit {
using units = std::tuple<baseUnits...>;
};
例如,如果我有这样的类型:
template <typename T>
struct Computations {
using collection_ratios = /* */
static constexpr ratios_product = /* */
}
我想知道我怎样才能:
derived_unit
的所有比率存储在像 std::arrayunits
的derived_unit
成员中的每个元素相乘的结果,并将其存储在具有折叠表达式(ratios_product)的变量中编辑:
T
是 derived_unit
的特化,例如:
struct MetersPerSecond :
public speed,
public derived_unit<
base_unit<Kilo, m>,
base_unit<Root, s>
>
{};
我不认为我得到了你的全部问题,但这部分是你正在寻找的吗?
// Abusing std::apply to make a new tuple type with only the ratio types.
// This avoids us having to mess with std::index_sequence, which can get confusing.
// std::declval is the MVP here - it allows us to fake values just in order
// to get the types we need.
template<typename T>
using ratios = decltype(std::apply([](auto const&... args){
return std::declval<std::tuple<decltype(args.r)...>>();
},
std::declval<T::units>()
));
template <typename T>
struct Computations {
using collection_ratios = ratios<T>;
// Testing our thing.
// Is the first item of the collection_ratios tuple, of type "Kilo"?
static_assert(std::is_same_v<
decltype(
std::get<0>(std::declval<collection_ratios>())
),
Kilo
>);
// It's not clear what you want here.
//static constexpr ratios_product = ???
};