Find largest element from N number of element using "Variadic Template". | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Find largest element from N number of element using "Variadic Template".

I want to create a template class which provides maximum element from a lit of another integers at compile time but I am getting error, please help me in solving the question.

17th Oct 2016, 12:10 PM
Paritosh Kumar Roy
Paritosh Kumar Roy - avatar
1 Answer
0
template <int N, int... Rest> int max() { int tmp = max<Rest...>(); return N < tmp ? tmp : N; } std::cout << max<3, 1, 4, 2, 5, 0>() << std::endl; template <int N, int... Rest> struct max_t { static int const value = max_t<Rest...>::value > N ? max_t<Rest...>::value : N; }; template <int N> struct max_t<N> { static int const value = N; }; template <int... NS> int max() { return max_t<NS...>::value; }
17th Aug 2017, 8:55 PM
Ahmet
Ahmet - avatar