Variadic templates c++17

Hi, I have been stuck on actually reversing the order of the sequences of exponents of 2 below for quite some time now. I hope someone is able to help. So, I should get an output from 1, 2, 4, 8.. etc. Not the reverse. Note: All of the meta() functions must be retained in the program below. Cannot remove!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>

template<size_t... Ns>
struct int_seq{}; 

template<size_t T, size_t...Ns>
struct sequence
{
	using type = typename sequence<
		T- 1,  1, 2 * Ns...
	>::type;
};

template<size_t...Ns>
struct sequence<0, Ns...>
{
	using type = sequence<Ns...>;
};

template<size_t T>
using create_int_seq  = typename sequence<T+ 1>::type;

template <typename T>
void print(T t)
{
	std::cout << t << " ";
}

template <typename T, typename... Ts>
void print(T t, Ts... ts)
{
	print(ts...);
	std::cout << t << " ";
}

template <
	template <size_t...> class T,
	size_t... S
>
void print_type(T<S...>)
{
	print(S...);
}
int main()
{
	constexpr size_t N = 63;
	create_int_seq<N> dum{};
	print_type(dum);

}
Last edited on
Can I have an understanding of why my program results in this specific error
"Error C1202 recursive type or function dependency context too complex"
What does it mean? And how do I solve it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include<iostream>

template<size_t... Ns>
class int_seq{}; 

template<size_t T, size_t...Ns>
struct sequence
{
	using type = typename sequence<
		T- 1, 2 * Ns... , 1
	>::type;
};

template<size_t...Ns>
struct sequence<0, Ns...>
{
	using type = sequence<Ns...>;
};

template<size_t T>
using create_int_seq  = typename sequence<T+ 1>::type;

template <typename T>
void print(T t)
{
	std::cout << t << " ";
}

template <typename T, typename... Ts>
void print(T t, Ts... ts)
{
	print(ts...);
	std::cout << t << " ";
}

template <
	template <size_t...> class T,
	size_t... S
>
void print_type(T<S...>)
{
	print(S...);
}
int main()
{
	constexpr size_t N = 63;
	create_int_seq<N> dum{};
	print_type(dum);

}
Last edited on
Reaches limit of size_t:
18446744073709551615 = (2 * 9223372036854775808) - 1
https://en.cppreference.com/w/c/types/size_t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>

template<size_t... Ns>
struct int_seq{}; // <----

template<size_t T, size_t...Ns>
struct sequence
{
    using type = typename sequence< T- 1, 1, 2 * Ns...>::type; // <----
};

template<size_t...Ns>
struct sequence<0, Ns...>
{
    using type = sequence<Ns...>;
};

template<size_t T>
using create_int_seq  = typename sequence<T+ 1>::type;

template <typename T>
void print(T t)
{
    std::cout << t << " ";
}

template <typename T, typename... Ts>
void print(T t, Ts... ts)
{
    print(ts...);
    std::cout << t << " ";
}

template <
    template <size_t...> class T,
    size_t... S
>
void print_type(T<S...>)
{
    print(S...);
}
int main()
{
    constexpr size_t N = 128; // <---
    create_int_seq<N> dum{};
    print_type(dum);

}



0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
9223372036854775808 4611686018427387904 2305843009213693952 
1152921504606846976 576460752303423488 288230376151711744 
144115188075855872 72057594037927936 
36028797018963968 
18014398509481984 9007199254740992 4503599627370496 
2251799813685248 1125899906842624 562949953421312 281474976710656 
140737488355328 70368744177664 35184372088832 17592186044416 
8796093022208 4398046511104 2199023255552 1099511627776 549755813888 
274877906944 137438953472 68719476736 34359738368 17179869184 8589934592 
4294967296 2147483648 1073741824 536870912 268435456 134217728 67108864 
33554432 16777216 8388608 4194304 2097152 1048576 524288 262144 131072 
65536 32768 16384 8192 4096 2048 1024 512 
256 128 64 32 16 8 4 2 1 
Program ended with exit code: 0
Are you taking a class, @sparki? I've never seen this stuff taught by an instructor.

Your first post builds the sequence correctly, but prints it in reverse.
Using the code in your first post, correct the problem by swapping line 32 and 33.
Last edited on
@sparki
A multi-templated/variadic/multi-method/recursive structure and program is no mean feat. :)
Registered users can post here. Sign in or register to post.