[英]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.