VasuOberoi's blog

By VasuOberoi, history, 3 years ago, In English

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.

  • Vote: I like it
  • -8
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by VasuOberoi (previous revision, new revision, compare).

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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)