Created
October 16, 2015 17:51
-
-
Save krysseltillada/718ccd1ee49da98d79f3 to your computer and use it in GitHub Desktop.
variadic template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> /// std::cerr | |
#include <cstdlib> /// #EXIT_SUCCESS | |
template <typename T> | |
void errorMsg (const T &t) { | |
std::cerr << t << std::endl; /// this template function will be called when the size of the pack is 1 because this function | |
} /// is the best match for the call because of its exactly one argument and is a more specialized function | |
template <typename T, typename ...Tv> /// template parameter pack expands its list of elements that deduced from template arguments and applies a pattern to the corresponding function parameter pack | |
void errorMsg (const T &t1, const Tv &...tv) { /// that is each element has const & Tv whatever type had deduced | |
std::cerr << t1 << std::endl; /// will print whatever type it is | |
errorMsg (tv...); /// calls the function itself and expands its remaining elements in pack t1 will be bound to the first elem of the pack which is string | |
} /// and the pack will be reduced depending on the size of the pack which is 3 and so on and so on until the remaining size of the pack is 1 | |
int main () | |
{ | |
errorMsg (2, "string", 2.33, 23.f, 's'); /// passing whatever type and number of arguments in the a template function | |
return EXIT_SUCCESS; /// system dependent return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment