gpt4 book ai didi

c++ - 将可变模板类的模板参数解包为常量和常量数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:24 26 4
gpt4 key购买 nike

我对 C++2011 的可变参数模板很陌生,我想知道是否存在执行以下操作的技巧:

template<typename T, unsigned int... TDIM> class VariadicTest
{
public:
static const unsigned int order_const = sizeof...(TDIM);
static const unsigned int size_const = // TDIM1*TDIM2*TDIM3...
static const unsigned int dim_const[order_const] = // {TDIM1, TDIM2, TDIM3...}
// if not possible :
// dim_const[64] = {TDIM1, TDIM2, TDIM3, 0, ..., 0}

};

做这样的事情有什么“技巧”吗?

非常感谢。

最佳答案

下面是另外两个功能的实现方式:

template<unsigned int... T> struct mul;
template<unsigned int L,unsigned int... T> struct mul<L,T...>
{
static const int val= L*mul<T...>::val;
};
template<unsigned int L> struct mul<L>
{
static const int val= L;
};

template<typename T, unsigned int... TDIM> class VariadicTest
{
public:
static const unsigned int order_const = sizeof...(TDIM);
static const unsigned int size_const = mul<TDIM...>::val;
static const unsigned int dim_const[order_const];
};
template<typename T, unsigned int... TDIM>
const unsigned int VariadicTest<T,TDIM...>::dim_const[order_const] = {TDIM...};

测试:http://liveworkspace.org/code/cfb0ec09a05931cfcc00edf29866e716

关于c++ - 将可变模板类的模板参数解包为常量和常量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11804760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com