int main(void) 
{
	if (pair_comparer(2, 2, 4, 4, 6, 6))
		LOG("equal pairs");

	if (!pair_comparer(3, 3, 4, 3))
		LOG("not equal pairs");

	/* 
	
		pair_comparer, accepts any number of arguments (even numbers) and returns true if
		and only if they are pair-wise equal. Everything that have the overloaded operator == can be used.

		pair_comparer(1.2, 1.2) compiles but
		pair_comparer(1.2, 1) does not: because float and int are not the same type.


		as said pair_comparer will only work for an even number of arguments because they are peeled off in pairs
		and the base case compares 2:
		pair_comparer(1,1,2) does not compile

		the compiler compains that the base case expects 2 arguments but only 1 is provided.

		we can add another base case that accepts 1 parameter
			bool pair_comparer(T single) {
				return false;
			}

		Here, we force all odd-numbered sequences of arguments to return false,
		because when only a single argument is left this version is matched.


		Performance:
			If you're concerned with the performance of code that relies on variadic templates, worry not. 
			As there's no actual recursion involved, all we have is a sequence of function calls pre-generated at compile-time. 
			This sequence is, in practice, fairly short (variadic calls with more than 5-6 arguments are rare). 
			Since modern compilers are aggressively inlining code, it's likely to end up being compiled to machine code that has absolutely no 
			function calls. What you end up with, actually, is not unlike loop unrolling.

			Compared to the C-style variadic functions, 
			this is a marked win, because C-style variadic arguments have to be resolved at runtime. 
			The va_ macros are literally manipulating the runtime stack. Therefore, variadic 
			templates are often a performance optimization for variadic functions.


	*/

}
Beispiel #2
0
int main()
{
    printer("hi %d %d %d\n", 4,5,6);
    std::cout << pair_comparer(5,5,7,8) << std::endl;
    std::cout << pair_comparer(5,5,7,7) << std::endl;
    long sum = adder(1, 2, 3, 8, 7);
    std::cout << sum << std::endl;   
    std::string s1 = "x", s2 = "aa", s3 = "bb", s4 = "yy";
    std::string ssum = adder(s1, s2, s3, s4);
    std::cout << ssum << std::endl;   

    std::vector<int> t1 { 1,2,3,4,5 };
    print_container(t1);
    std::map<int, char *> t2 { {5, "hi"}, {4, "bye"}, {1, "try" }};
    print_container(t2);
}
Beispiel #3
0
bool pair_comparer(T a, T b, Args... args) {
  return a == b && pair_comparer(args...);
}