Skip to content

[P0870R6] A proposal for a type trait to detect narrowing conversions #5

@saki7

Description

@saki7

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p0870r6.html

template<class From, class To>
concept is_convertible_without_narrowing = std::is_convertible_v<From, To> && 
  requires (From&& x) {
    { std::type_identity_t<To[]>{std::forward<From>(x)} } -> std::same_as<To[1]>;
  };

I think our version (quoted below) can be rewritten with the standard utility above, but we must make sure that our test suite passes, and also that the IDE completion will not fail

// This version works better than MSVC's, does not break IntelliSense or ReSharper
template<std::size_t I, class Ti>
struct aggregate_initialize_overload
{
using TiA = Ti[];
// https://eel.is/c++draft/dcl.init.general#14
// https://eel.is/c++draft/dcl.init.list#3.4
// https://eel.is/c++draft/dcl.init.aggr#3
template<class T>
auto operator()(Ti, T&&) -> aggregate_initialize_tag<I, Ti>
requires requires(T&& t) { { TiA{std::forward<T>(t)} }; } // emulate `Ti x[] = {std::forward<T>(t)};`
{
return {}; // silence MSVC warning
}
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions