C++ Logo

std-proposals

Advanced search

Re: type_traits and similar types

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Thu, 9 Jan 2020 04:02:43 +0300
On 07/01/2020 00:40, Arthur O'Dwyer wrote:
> On Sun, Jan 5, 2020 at 8:24 PM language.lawyer--- via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Why not to add a trait to check if two types are similar?
>> (https://timsong-cpp.github.io/cppwp/n4659/conv.qual#def:similar_types)
>>
>
> That would be something like this, yes?
> https://godbolt.org/z/aZU2q6
>
> What use would that be? Nothing in the standard library (or in any
> user-defined library I can think of) depends on being able to tell if two
> types are "similar," does it?

Well, unique_ptr<T[], D> could benefit from such a trait, see http://wg21.link/n4042 and http://wg21.link/n4089
However, they managed to live without such a trait, relying on is_convertible.
And indeed, prvalue expression convertibility relies on type similarity https://timsong-cpp.github.io/cppwp/n4659/conv.qual#3
I think it would be valid to define is_similar_v as

template<typename T, typename U>
inline constexpr bool is_similar_v = std::is_scalar_v<T> && std::is_scalar_v<U> && (std::is_convertible_v<T, U> || std::is_convertible_v<U, T>);

Received on 2020-01-08 19:05:20