I am learning vardiac functions and i See two different ways of writing vardiac functions.I m confused which one is best and whats the actual difference between them.
// CODE ---------------------------------------------------------------------------------------
include<bits/stdc++.h>
using namespace std;
// TYPE 1 template<typename... T> void print1(T... args) { ((cout << args << " "), ...); }
// Base empty function void print2() { cout << "\n"; return; }
// TYPE 2 template<typename Head, typename... Tail> void print2(Head H, Tail... args) { cout << H << " "; print2(args...); }
int main() { int x = 5; int y = 6; int z = 7; int r = 4; print1(x, y, z, r); print2(x, y, z, r);
}
// CODE--------------------------------------------------------------------------------------------------------------------
Print 1 and Print 2 both functions accept variable no of arguments.Both will print exact same ans.But in print2 function we are use something like a base empty function Which preserves for last call to the function in call stack.In print1 function We are not using any base function.I want to know the difference between these two functions.
Auto comment: topic has been updated by VasuOberoi (previous revision, new revision, compare).
First function use some magic known as fold expressions, and works starting with C++17
Second function works starting with C++11 (or where started variardic templates)
I'd prefer second function as I can't remember how works fold expressions)