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.↵
↵
↵
↵
↵
// 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.↵
↵
↵