Variadic introducing an static for each typename passed

Mar 24, 2016 at 1:21am
Hi,

Having the following variadic template definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename...Tuples>
struct expand;

template<typename T, typename...Tuples>
struct expand<T, Tuples...> : expand<Tuples...>
{
	static T element;
};

template<typename T>
struct expand<T> {
	using type = T;
};


and putting aside for the moment the fact that the static variable will show as missing by the linker, if you invoke it like so:

std::cout << expand<int, double, long>::element << std::end;

wouldn't there have to be 3 static variables with types:
int, double, long
?

How could one access them?

Best regards,
Juan
Mar 24, 2016 at 4:06am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

template < typename... > struct expand ;

template < typename T, typename... REST > struct expand< T, REST... > : expand<REST...>
{ using base = expand<REST...> ; static T element ; };

template < typename T > struct expand<T> { static T element ; };

int main()
{
    std::cout << std::boolalpha
              << expand<int,double,bool,long>::element << '\n' // 30 + sizeof(int)
              << expand<int,double,bool,long>::base::element << '\n' // 20 + sizeof(double)
              << expand<int,double,bool,long>::base::base::element << '\n' // true // bool(10 + sizeof(long)
              << expand<int,double,bool,long>::base::base::base::element << '\n' ; // sizeof(long)
}

template < typename T, typename... REST > T expand<T,REST...>::element = T( sizeof...(REST) * 10 + sizeof(T) ) ;
template < typename T > T expand<T>::element = sizeof(T) ;

http://coliru.stacked-crooked.com/a/cf96edb3fc2197fa
Topic archived. No new replies allowed.