I came across with this code in an std::optional
implementation :
template <class T, class U>
struct is_assignable
{
template <class X, class Y>
constexpr static bool has_assign(...) { return false; }
template <class X, class Y, size_t S = sizeof((std::declval<X>() = std::declval<Y>(), true)) >
// the comma operator is necessary for the cases where operator= returns void
constexpr static bool has_assign(bool) { return true; }
constexpr static bool value = has_assign<T, U>(true);
};
The part that I cant understand how it works or how it is evaluated is size_t S = sizeof((std::declval<X>() = std::declval<Y>(), true))
I know that if the assign operation fails it will fall back to the first definition of has_assign that returns false, but i don't know why it has the , true)
part.
I did some test with structs that returns void on the assign operator and removing the , true
part in sizeof
gives me the same results.