简体   繁体   English

使用 std::index_sequence 使用许多模板参数配置的递归聚合类型

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

There is a class template:有一个类模板:

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

All the instantiations of Feature<...> are 'collected' here: Feature<...>所有实例都在此处“收集”:

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

All the features are created as follows:所有功能的创建如下:

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

So far so good.到现在为止还挺好。 My task is to get rid of those hard coded values in there 1..3, 4..6 etc. The way to do so is to have generated header file which contains the configuration for all the features.我的任务是摆脱 1..3、4..6 等中的那些硬编码值。这样做的方法是生成包含所有功能配置的头文件。 Something like:就像是:

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 };
};

Then I need to change type definition of FeatureConfig somehow to use the specializations of FeatureConfig based on an index (0, 1, ...).然后我需要以某种方式更改FeatureConfig类型定义,以使用基于索引 (0, 1, ...) 的FeatureConfig化。 My unsuccessfull try is:我不成功的尝试是:

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;

It seems I am somehow mixing type and value...似乎我以某种方式混合了类型和价值......

Many thanks in advance to anyone willing to help me fix the incorrect code in my last listing up there.非常感谢任何愿意帮助我修复上次列表中错误代码的人。

Cheers Martin干杯马丁

If I understand correctly what do you want...如果我理解正确,你想要什么......

I propose the declaration (no definition required because is used only inside a decltype() ) of the following function我提出以下函数的声明(不需要定义,因为仅在decltype()内使用)

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

Now you can define FeatureConfig simply as follows现在你可以简单地定义FeatureConfig如下

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

The following is a full compiling (simplified) example下面是一个完整的编译(简化)示例

#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, "!" );
 }

If I understand correctly how do you use Config (if t1 is ever N*3u , if if t2 is ever 1u+N*3u and if t3 is ever 2u+N*3u ), you can avoid Config at all and write getFeaturesType as follows如果我正确理解您如何使用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.

相关问题 Eclipse CDT 在使用 std::index_sequence 时报告错误错误 - Eclipse CDT is reporting false erros while using std::index_sequence 使用 std::index_sequence 初始化具有固定大小数组成员的 POD 结构容器 - using std::index_sequence to initialize POD struct container with fixed size array members 在 std::index_sequence 中没有对包扩展的诊断 - No diagnostic for pack expansion in std::index_sequence 默认情况下匹配std :: index_sequence - Matching std::index_sequence by default argument 如何根据 function 特征将 std::index_sequence 参数传递给嵌套模板结构 - How to pass std::index_sequence param to an nested-template struct according to function traits 将std :: tuple解压缩到原位,而无需使用std :: index_sequence的人工层 - Unpack std::tuple in-place without the artificial layer for std::index_sequence 禁止对 index_sequence 发出警告“表达式结果未使用” - Suppress warning “expression result unused” on a index_sequence 使用模板类型作为 std::invoke 的参数 - Using template type as argument to std::invoke 从 std::integer_sequence 调用带有模板参数的模板 - Calling a template with template parameters from a std::integer_sequence 使用模板函数类型和 std::function 之间的区别 - difference between using a template function type and std::function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM