Help me with this logic applied in code

here i was not able to understand what logic has been applied and how this code is able to find the smaller number among the two without comparing?? i mean if i would have done it, then i would has asked my code to compare num1 and num2 ( with <, > sign ) and whosoever has been greater, i would have printed it with std::cout.
this question is from Codeacademy course of C++ “how to write functions” part 8/9.

template <typename T> T get_smallest(T num1, T num2) { return num2 < num1? num2 : num1; }

Hi,

What’s happening here is that a ternary operator is being used.

The format is basically

(bool) ? return_this_val_if_true : return_this_val_if_false;

num2 < num1 returns a bool, it will return num2 if it’s smaller than num1, and if not it will return num1 (logically, it will return num1 if num1 is smaller or equal to num2.

Reference: Other operators - cppreference.com

2 Likes