C++ Weekly - Ep 64 - C++11's std::min (and my version)

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

gcc 7.1 generates code much more comparable to clang. Though that of course gives you no good excuses to create a variadic template min. :-) I think the reason the C++ standards people chose to create the initializer list version instead of the variadic template version is precisely because of the type confusion the variadic template version allows.

Omnifarious
Автор

It is matter of preference but I would go with more straightforward/natural implementation which only requires C++11 compiler:

template<typename T>
const T& variadic_min(const T& val1, const T& val2)
{
return std::min<T>(val1, val2);
}

template<typename T0, typename T1, typename... R>
typename std::common_type<T0, T1, R...>::type variadic_min(const T0& val1, const T1& val2, const R&... other)
{
return variadic_min(variadic_min(val1, val2), other...);
}

dshvets
Автор

BTW the "move out" in the assembly code is in fact the %eax register. EAX is the register used to return values in the stdcall, cdecl and some other conventions. The syntax from the clang output at this video is AT&T, differently from Intel syntax used in the GCC ones the register order is inversed: mov eax, esi (intel) -> movl %esi, %eax (at&t)

fungosbauux
Автор

Why do we use pointers here? In particular, why are you taking the address of the return value of std::min?

stephenry
Автор

nice video, I wonder if using `std::common_type` to determine a return type would be a good idea. Returning by reference will be out of the question in this case of course.
On the other side it would be nice to have rvalue reference version that could forward temporaries...

AxelStrem
Автор

using std::addressof() instead of "&" is better way :)

avisditiu
Автор

this video is so helpful, but you can tell me know, what 's kind of IDE that you use in this video ?, thank you so much ?

LamNguyen-nwhl