[Bug c++/96575] New: std::ranges::sort is not usable as a 'constexpr' function when saving its return value in lambda function

hewillk at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Aug 12 01:23:58 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96575

            Bug ID: 96575
           Summary: std::ranges::sort is not usable as a 'constexpr'
                    function when saving its return value in lambda
                    function
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

(https://godbolt.org/z/W8zx58)

#include <array>
#include <algorithm>

constexpr auto f(auto algo) {
  return [=] {
    // this one is okay
    // algo(std::array{1, 0});
    // this one is also okay
    // auto it = algo(std::array{0, 1});
    auto it = algo(std::array{1, 0});
    return 0;
  }();
}

int main()
{
  static_assert(f(std::ranges::sort) == 0); 
}


This fails on gcc 10.2 and trunk with:

<source>:13:23: error: 'constexpr auto f(auto:16) [with auto:16 =
std::ranges::__sort_fn]' called in a constant expression
   13 |   constexpr auto i = f(std::ranges::sort);
      |                      ~^~~~~~~~~~~~~~~~~~~
<source>:4:16: note: 'constexpr auto f(auto:16) [with auto:16 =
std::ranges::__sort_fn]' is not usable as a 'constexpr' function because:
    4 | constexpr auto f(auto algo) {
      |                ^
<source>:8:8: error: call to non-'constexpr' function
'f<std::ranges::__sort_fn>::<lambda()>'
    5 |       return [=] {
      |              ~~~~~
    6 |         auto it = algo(std::array{1, 0});
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    7 |           return 0;
      |           ~~~~~~~~~
    8 |       }();
      |       ~^~
<source>:5:14: note: 'f<std::ranges::__sort_fn>::<lambda()>' is not usable as a
'constexpr' function because:
    5 |       return [=] {


More information about the Gcc-bugs mailing list