Last active
January 10, 2019 11:19
-
-
Save kkabdol/09e6f4ca69bd46e98c86593403db010e to your computer and use it in GitHub Desktop.
Template Parameter Pack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://kevinushey.github.io/blog/2016/01/27/introduction-to-c++-variadic-templates/ | |
// https://en.cppreference.com/w/cpp/language/parameter_pack | |
// https://www.hackerrank.com/challenges/cpp-variadics/problem | |
#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"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment