簡體   English   中英

使用 std::index_sequence 使用許多模板參數配置的遞歸聚合類型

[英]Recursive aggregate type configured using many template parameters using std::index_sequence

有一個類模板:

template<std::size_t ID, std::size_t T1, std::size_t T2, std::size_t T3>
class Feature { /* Implementation goes here */ };

Feature<...>所有實例都在此處“收集”:

template<typename FEATURE, typename... OTHERS>
class Features<FEATURE, OTHERS...> : public Features<OTHERS...> {
public:
    /* Operations defined here */
private:
    FEATURE m_feature;
};

所有功能的創建如下:

using FeatureConfig = Features<Feature<0, 1, 2, 3>, Feature<1, 4, 5, 6>>;
FeatureConfig m_features;

到現在為止還挺好。 我的任務是擺脫 1..3、4..6 等中的那些硬編碼值。這樣做的方法是生成包含所有功能配置的頭文件。 就像是:

template<std::size_t> struct Config;

template<>
struct Config<0> {
    static constexpr std::size_t t1 { 1 };
    static constexpr std::size_t t2 { 2 };
    static constexpr std::size_t t3 { 3 };
};

template<>
struct Config<1> {
    static constexpr std::size_t t1 { 4 };
    static constexpr std::size_t t2 { 5 };
    static constexpr std::size_t t3 { 6 };
};

然后我需要以某種方式更改FeatureConfig類型定義,以使用基於索引 (0, 1, ...) 的FeatureConfig化。 我不成功的嘗試是:

template<std::size_t... INDEX_SEQUENCE>
using FeatureConfig = Features<Feature<INDEX_SEQUENCE, Config<INDEX_SEQUENCE>::t1, Config<INDEX_SEQUENCE>::t2, Config<INDEX_SEQUENCE>::t3>...>;

FeatureConfig<std::make_index_sequence<2>> m_features;

似乎我以某種方式混合了類型和價值......

非常感謝任何願意幫助我修復上次列表中錯誤代碼的人。

干杯馬丁

如果我理解正確,你想要什么......

我提出以下函數的聲明(不需要定義,因為僅在decltype()內使用)

template <std::size_t ... Is>
auto getFeaturesType (std::index_sequence<Is...>)
   -> Features<Feature<Is, Config<Is>::t1, Config<Is>::t2, Config<Is>::t3>...>;

現在你可以簡單地定義FeatureConfig如下

template <std::size_t N>
using FeatureConfig
   = decltype(getFeaturesType(std::make_index_sequence<N>{})); 

下面是一個完整的編譯(簡化)示例

#include <type_traits>
#include <utility>

template <std::size_t, std::size_t, std::size_t, std::size_t>
struct Feature { };

template <typename...>
struct Features
 { };

template <typename F, typename... Os>
struct Features<F, Os...> : public Features<Os...>
 { F m_feature; };

template <std::size_t N>
struct Config
 {
   static constexpr std::size_t t1 { N*3u };
   static constexpr std::size_t t2 { 1u + N*3u };
   static constexpr std::size_t t3 { 2u + N*3u };
 };

template <std::size_t ... Is>
auto getFeaturesType (std::index_sequence<Is...>)
   -> Features<Feature<Is, Config<Is>::t1, Config<Is>::t2, Config<Is>::t3>...>;

template <std::size_t N>
using FeatureConfig
   = decltype(getFeaturesType(std::make_index_sequence<N>{}));

int main () 
 {
   using T1 = FeatureConfig<2u>;
   using T2 = Features<Feature<0u, 0u, 1u, 2u>, Feature<1u, 3u, 4u, 5u>>;

   static_assert( std::is_same<T1, T2>::value, "!" );
 }

如果我正確理解您如何使用Config (如果t1永遠是N*3u ,如果t2永遠是1u+N*3u並且如果t3永遠是2u+N*3u ),您可以完全避免Config並將getFeaturesType寫為跟隨

template <std::size_t ... Is>
auto getFeaturesType (std::index_sequence<Is...>)
   -> Features<Feature<Is, Is*3u, Is*3u+1u, Is*3u+2u>...>;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM