#include <iostream>
template<typename T>
bool pair_comparer(T a, T b) {
return a != b;
}
template<typename T, typename... Args>
bool pair_comparer(T a, T b, Args... args) {
return a == b && pair_comparer(args...);
}
int main() {
bool areSame = pair_comparer(1, 1, 2, 2, 6, 6);
std::cout << "areSame " << areSame << endl; // prints 0
return 0;
}
I don't expect the example to compile , but it is . How is the call to pair_comparer
with two arguments resolved here ? Is there something I am missing
In the first call to pair_comparer
you have:
a = 1 , b = 1, and args = [2, 2, 6, 6]
and then you have a recursive call to pair_comparer
and this time you have:
a = 2 , b = 2, and args = [6, 6]
finaly you have a call to pair_comparer(T a, T b)
with: args = [6, 6]
But you have to put return a == b;
instead of return a != b;
in bool pair_comparer(T a, T b)
to get the right answer. it means all the numbers are equal by pair
But if you start to call by two arguments only:
For example with:
bool areSame = pair_comparer(6, 6);
The program will call directly: pair_comparer(T a, T b) because it is the first match (we have only 2 parameters and not more)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.