Answer the question
In order to leave comments, you need to log in
Variadic template c++?
#include <iostream>
using namespace std;
//от сюда
template <bool a> int reversed_binary_value() {
return a;
}
template <bool a, bool b, bool... d> int reversed_binary_value() {
return (reversed_binary_value<b, d...>() << 1) + a;
}
//до сюда
template <int n, bool...digits>
struct CheckValues {
static void check(int x, int y)
{
CheckValues<n-1, 0, digits...>::check(x, y);
CheckValues<n-1, 1, digits...>::check(x, y);
}
};
template <bool...digits>
struct CheckValues<0, digits...> {
static void check(int x, int y)
{
int z = reversed_binary_value<digits...>();
std::cout << (z+64*y==x);
}
};
int main()
{
int t; std::cin >> t;
for (int i=0; i!=t; ++i) {
int x, y;
cin >> x >> y;
CheckValues<6>::check(x, y);
cout << "\n";
}
}
Answer the question
In order to leave comments, you need to log in
This template evaluates at compilation to:
rbv<false, false, true, true>() = 1100 2 .
And it consists of two parts.
1. For one parameter, we have directly written a template.
2. For false, false, true, true, we use the second pattern: a=false, b=false, d = (true, true).
And he equals (rbv<false, true, true> << 1) + false
.
To compute a new rbv, the second pattern works again: a = false, b = true, d = (true).
And he equals (rbv<true, true> << 1) + false
.
For the third rbv we have a = true, b = true, d = ().
Attention, the list d may be empty. Therefore, so that there is no conflict with the first template, the second one is written for two or more parameters.
And our third value is (rbv<true> << 1) + true
.
Here the first template works and it turns out 11 2 .
Further, it is already possible to calculate all rbv in turn and get 1100 2 .
A sort of recursion at the compilation stage. For example, initially the template is specified with n parameters (... just implies an unknown number of parameters). If n==1, then the first function is selected and the result is returned immediately. Otherwise, n > 1 and the second function is chosen. All template parameters are broken down according to the principle parameter 1, parameter 2, whatever is left. Parameter 2(b) is needed in this case so that there is no conflict at n == 1. Next, the template is specified again, with fewer parameters. By induction, we obtain the finiteness of this process.
In modern C ++ is not strong, so there may be errors in terms.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question