Skip to content

[libc++] Hard errors caused by narrowing conversions in list-initialization are suppressed #155962

@frederick-vs-ja

Description

@frederick-vs-ja

Conversions functions of ranges algorithm result types are specified to copy-list-initialize the results ([algorithms.results]).

As a result, narrowing conversions should cause hard errors in the functions. However, Clang seems to suppress the errors due to #pragma GCC system_header. Godbolt link.

#include <algorithm>
#include <concepts>

template <class I1>
struct my_in_found_result {
  [[no_unique_address]] I1 in;
  bool found;

  template <class I2>
    requires std::convertible_to<const I1&, I2>
  constexpr operator my_in_found_result<I2>() const& {
    return {in, found};
  }

  template <class I2>
    requires std::convertible_to<I1, I2>
  constexpr operator my_in_found_result<I2>() && {
    return {std::move(in), found};
  }
};

int main(int, char**) {
  {
    std::ranges::in_found_result<double> res{10, true};
    std::ranges::in_found_result<int> res2 [[maybe_unused]] = res; // hard error suppressed
  }
  {
    my_in_found_result<double> res{10, true};
    my_in_found_result<int> res2 [[maybe_unused]] = res; // correctly rejected
  }
}

Although it might deserve an LWG issue to fix the constraints, i.e. either accept narrowing conversions by not using list-initialization, or constrain harder to reject narrowing conversions in an SFINAE-friendly way, it seems to me that we shouldn't suppress -Wc++11-narrowing in standard library headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepts-invalidlibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions