访问另一个别名中的个别别名,这是一个带有折叠表达式的参数包

问题描述 投票:0回答:1

鉴于这种类型:

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::array 这样的容器中,并带有折叠表达式 (collection_ratios)
  • 存储
    units
    derived_unit
    成员中的每个元素相乘的结果,并将其存储在具有折叠表达式(ratios_product)的变量中

编辑:

T
derived_unit
的特化,例如:

struct MetersPerSecond :
   public speed,
   public derived_unit<         
        base_unit<Kilo, m>,
        base_unit<Root, s>
   >
{};
c++ variadic-templates fold-expression
1个回答
0
投票

我不认为我得到了你的全部问题,但这部分是你正在寻找的吗?

// 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 = ???
};
© www.soinside.com 2019 - 2024. All rights reserved.