[llvm-bugs] [Bug 50137] New: ad hoc requires-expression being evaluated too eagerly

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Apr 26 23:24:01 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50137

            Bug ID: 50137
           Summary: ad hoc requires-expression being evaluated too eagerly
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: pilarlatiesa at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

The following program prints: "Overridden", "Fallback":

```cpp
#include <iostream>
#include <type_traits>

template<typename T>
class Base
{
private:
  static constexpr bool IsOverridden = requires(T const x) { x.template
f<std::true_type{}>(); };

public:
  template<std::false_type = std::false_type{}> requires IsOverridden
  void f() const
    { static_cast<T const *>(this)->f(); }

  template<std::false_type = std::false_type{}>
  void f() const
    { std::cout << "Fallback" << std::endl; }
};

class A : public Base<A>
{
public:
  template<std::true_type = std::true_type{}>
  void f() const
    { std::cout << "Overridden" << std::endl; }
};

class B : public Base<B> {};

template<typename T>
void f(Base<T> const &x)
  { x.f(); }

int main()
{
  A const a;
  B const b;

  f(a);
  f(b);

  return 0;
}
```

However, if we remove the bool constant and use the requires-expression
directly in the requires-clause, it prints "Fallback", "Fallback":

```cpp
#include <iostream>
#include <type_traits>

template<typename T>
class Base
{
public:
  template<std::false_type = std::false_type{}>
    requires requires(T const x) { x.template f<std::true_type{}>(); };
  void f() const
    { static_cast<T const *>(this)->f(); }

  template<std::false_type = std::false_type{}>
  void f() const
    { std::cout << "Fallback" << std::endl; }
};

class A : public Base<A>
{
public:
  template<std::true_type = std::true_type{}>
  void f() const
    { std::cout << "Overridden" << std::endl; }
};

class B : public Base<B> {};

template<typename T>
void f(Base<T> const &x)
  { x.f(); }

int main()
{
  A const a;
  B const b;

  f(a);
  f(b);

  return 0;
}
```

GCC consistently prints "Overridden", "Fallback" in both cases, which I believe
is what makes sense. Please let me know if it's a bug, and otherwise I'll
report it to GCC.

Thanks
Pili

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210427/a1f4fea6/attachment-0001.html>


More information about the llvm-bugs mailing list