I am having fun with the fold expressions to understand better where I will be able to use them in my projects. So I choose to initialize them with a simple
(datas = ... = 1);
So far everything work as expected, every value is at 1. Then I tryed to use the left shift initialization so:
(datas = ... = 1);
(datas <<= ...);
Here is also work as I expect it, it increase by a power of 2 it's nice. And finally I tryed this:
(datas = ... = 1);
(datas >>= ...);
And it give me as output a 0, 1, 0, 1, 0, 1, ...
, I expected it would be all at 0. So here the code:
#include <iostream>
template<typename... T>
void test(T&&... datas) {
(datas = ... = 1);
(datas >>= ...);
(std::cout << ... << (std::to_string(datas) + " ")) << std::endl;
}
int main(int argc, char const *argv[])
{
int v1, v2, v3, v4;
test(v1, v2, v3, v4);
return 0;
}
Why it is 0, 1, 0, 1
etc instead of 0, 0, 0, 0
? I through for every argument it will do more something like that:
arg1 >>= arg2, arg2 >>= arg3, arg3 >>= arg4, etc
but it's true that then the last value should be 1 then so it could also be 0, 0, 0, 1
.