Variadic templates c++17

Jul 28, 2020 at 8:20am
Hi, I have implemented the following code below on variadic templates for creating exponents of 2 based on c++17 standard, however, my lecturer has thrown in additional constraints and said that I must not use any (*) multiplication operators and (+) addition operators in my code... May I know what I can do to make a change in the line return seq_t<Count_t - 1, 1, 2 * Rest...>(); below such that I am able to generate exponents of 2, without using (*) operator and (+) operator. Note: code should remain the same way if possible. Only change the operators

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
#include<iostream>
 
template<size_t... Args>
struct seq{};
template <size_t Count_t, size_t... Rest>
auto seq_t()
{
	if constexpr (Count_t == 0)  
		return seq<Rest...>();

	else  
		return seq_t<Count_t - 1, 1, (2*n)...>();
}
 
template <size_t Count_t>
using type_m = decltype(seq_t<Count_t + 1>());

template<size_t... Args>
void print(seq<Args...>&& )
{
	for (const auto& i : { Args... })
		std::cout << i << " "; 
}
int main()
{
	print(type_m<63>());
}
Last edited on Jul 29, 2020 at 9:48am
Jul 28, 2020 at 10:47am
Is anyone able to help? Still can't figure it out..
Jul 28, 2020 at 10:52am
1
2
3
4
5
6
#include<iostream>

int main()
{
   for ( unsigned long long i = 0; i < 64; i++ ) std::cout << i << '\t' << ( 1ull << i ) << '\n';
}
Last edited on Jul 28, 2020 at 10:53am
Jul 28, 2020 at 11:04am
Yes, but I need to keep the use of meta functions in my program above, its the logic that needs changed, but i'm not sure how...

To be more specific, What can I replace the line return seq_t<Count_t - 1, 1, 2 * Rest...>(); above such that I am able to generate exponents of 2 but note that I can't use the (*) and (+) operators. And keeping the rest of the program intact as that is one of the requirements.

I tried doing this:

return seq_t<Count_t - 1, 1, 1<<(Rest)...>(); but doesn't work.

Hope you can help thanks!
Last edited on Jul 28, 2020 at 11:11am
Jul 28, 2020 at 11:34am
What does your assignment actually say? (Not your rewording of it - the original).

I can't see any good use for template functions here - variadic or otherwise.
Jul 28, 2020 at 11:48am
Well, the assignment is about parameter packs & sequence types . It will need to generate/ create sequence , then extract sequences from the types & then display them . It must generate powers of 2. I have done all that, but only can't figure out the problem as stated above.
Last edited on Jul 29, 2020 at 9:47am
Jul 28, 2020 at 12:35pm
Let me ask the question again!
What does your assignment actually say?
Jul 28, 2020 at 1:38pm
It says the above? I re-worded very little. "The assignment is about parameter packs & sequence types. It will need to generate/ create sequence , then extract sequences from the types . It must generate powers of 2. It must not contain any (*) multiplication or (+) addition operators"
Last edited on Jul 29, 2020 at 9:46am
Jul 28, 2020 at 1:47pm
What code has your lecturer insisted that you start with?
Last edited on Jul 28, 2020 at 2:04pm
Jul 28, 2020 at 2:19pm
The code I posted above, I followed my professor's code sample, and modify it to make it generate the sequence of powers of 2. However, Now he said it can't contain any * operators and + operators. He said we MUST follow his style.
Last edited on Jul 28, 2020 at 2:30pm
Jul 28, 2020 at 3:06pm
return seq_t<Count_t - 1, 1, (Rest << 1)...>();

What an idiotic assignment.
Last edited on Jul 28, 2020 at 3:06pm
Jul 28, 2020 at 3:21pm
Thanks for your help mbozzi. Oh man, I was quite close. This is a little confusing. Just to clarify, what you are doing is left shifting the bit first, then unpack the arguments right? I agree with you though. I hate the restrictions he imposes. Its very unnecessary. But what am I to do. Anyway, now, how do I eliminate the (+) operator within Count_t + 1 as shown below, what can I replace it with, whilst still keeping the program intact. Because as he said he doesn't allow for * operator and + operator. If only it wasn't variadic templates.. it would be much easier... Not intuitive enough for me as I am new to this.
1
2
template <size_t Count_t>
using type_m = decltype(seq_t<Count_t + 1>());
Last edited on Jul 28, 2020 at 3:30pm
Jul 28, 2020 at 4:30pm
Just to clarify, what you are doing is left shifting the bit first, then unpack the arguments right?

Yes, we just replace * 2 with << 1 - these are equivalent operations in this case.

Anyway, now, how do I eliminate the (+) operator within Count_t + 1 as shown below
Just erase it: it doesn't seem to be necessary.

By the way, most of the stuff you need is already in the standard library:
1
2
3
4
5
6
7
8
9
10
template <std::size_t... Is>
  constexpr auto powers_of_2(std::index_sequence<Is...>) 
  { 
    return std::index_sequence<(std::size_t{1} << Is)...>{}; 
  }

int main()
{
    print(powers_of_2(std::make_index_sequence<6>()));
}
Last edited on Jul 30, 2020 at 10:42pm
Jul 28, 2020 at 4:39pm
Hi mbozzi, thanks for the reply! I can't just erase the + operator, otherwise my output would be 1 number short. After I erased, the print out stopped at 4611686018427387904 instead of 9223372036854775808. I need to print the latter as well, to match my lecturer's test case. Are there any other ways to go about handling this? Hope you can help. Thank you.

With regards to the standard library, my lecturer wants us to use those function definitions that you see above for the assignment; It's not a case whereby I do not want to be more efficient or anything. So I am limited in that sense.
Last edited on Jul 28, 2020 at 4:43pm
Jul 28, 2020 at 4:53pm
Get rid of the +1 and say

1
2
3
4
if constexpr (Count_t == 0)  
	return seq<1, Rest...>();
else  
	return seq_t<Count_t - 1, 2, (Rest << 1)...>();
Topic archived. No new replies allowed.