C++ Riddle - Feb-02, 2022


While implementing a variadic max function, with the signature:

template<class T, class... Ts>
auto my_max(const T& t, const Ts&... ts) { /* ... */ }

It appears that gcc and clang disagree on the question of who is bigger: welcome or hi:

Explain this weird result, who is right? can it be both?


No one is right or wrong the comparation between “welcome” and “bye” is address comparation and in our example the address layout of “welcome” under gcc or clang is smaller/bigger then “bye” which explain the difference between the compilers. After that the result is compared with std::string (“hi”) which means that the previous result is implicit cast to std::string and the 2 strings are compared.