简体   繁体   中英

Unpacking template parameters of a variadic templated class to constants and array of constants

I'm quite new to the variadic template of C++2011 and I would like to know if a trick exists to do the following thing :

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}

};

Is there any "trick" to do a such thing ?

Thank you very much.

Here is the way to implement other two functionality:

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

Test : http://liveworkspace.org/code/cfb0ec09a05931cfcc00edf29866e716

Here's a partial answer, it does order_const and size_const . But I can't see how to do dim_const yet.

#include<iostream>
using namespace std;

template<typename T, unsigned int... TDIM>
class VariadicTest;

template<typename T>
class VariadicTest<T>
{
    public:
        static const unsigned int order_const = sizeof...(TDIM);

        static const unsigned int size_const = 1;
};
template<typename T, unsigned int baseTDIM, unsigned int... others>
class VariadicTest<T, baseTDIM, others...>
{
    public:
        static const unsigned int order_const  = sizeof...(TDIM);
        static const unsigned int size_const = baseTDIM * VariadicTest<T,others...> :: size_const;    
};

int main() {
        VariadicTest<double, 9, 4, 5> x;
        cout << x.order_const << endl;
        cout << x.size_const << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM