llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Partial ordering of constrained member functions

Open PiliLatiesa opened this issue 3 years ago • 6 comments

template<typename>
constexpr bool b = true;

template<typename T>
concept C = b<T>;

template<typename T, typename U>
class foo
{
public:
  void bar() const requires C<T> {} // #1
  void bar() const requires (C<T> && C<U>) {} // Subsumes #1
};

template<typename T, typename U>
void bar(foo<T, U> const &) requires C<T> {} // #2

template<typename T, typename U>
void bar(foo<T, U> const &) requires (C<T> && C<U>) {} // Subsumes #2

int main()
{
  foo<int, int> x;
  x.bar(); // Clang reports an ambiguity
  bar(x);  // Clang does not complain
  return 0;
}

PiliLatiesa avatar Oct 05 '22 14:10 PiliLatiesa

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Oct 05 '22 14:10 llvmbot

gobolt showing clang rejects and gcc accepts the code: https://godbolt.org/z/n1GsYKvGs

@erichkeane asked me what MSVC says and it agree with clang: https://godbolt.org/z/odc4E7h4e

shafik avatar Oct 05 '22 16:10 shafik

If the member functions are templates:

template<typename>
constexpr bool b = true;

template<typename T>
concept C = b<T>;

template<typename T, typename U>
class foo
{
public:
  template<typename = void>
  void bar() const requires C<T> {} // #1

  template<typename = void>
  void bar() const requires (C<T> && C<U>) {} // Subsumes #1
};

template<typename T, typename U>
void bar(foo<T, U> const &) requires C<T> {} // #2

template<typename T, typename U>
void bar(foo<T, U> const &) requires (C<T> && C<U>) {} // Subsumes #2

int main()
{
  foo<int, int> x;
  x.bar(); // Clang reports an ambiguity
  bar(x);  // Clang does not complain
  return 0;
}

then Clang trunk accepts the testcase, but Clang 15 rejects it.

Perhaps @erichkeane 's patch on deferred evaluation of constraints made a difference here.

PiliLatiesa avatar Oct 13 '22 09:10 PiliLatiesa

Maybe? We HAVE had work done in the partial ordering of constrained functions since 15 though.

erichkeane avatar Oct 13 '22 12:10 erichkeane

See https://reviews.llvm.org/D128750 Perhaps @yuanfang-chen has some opinions on it?

erichkeane avatar Oct 13 '22 13:10 erichkeane

I think the test case should be accepted.

When picking the overloaded non-template function, constraints partial ordering applies by https://eel.is/c++draft/over.match.best#general-2.6

yuanfang-chen avatar Oct 14 '22 01:10 yuanfang-chen

I believe this has been already fixed.

El vie, 14 oct 2022 a las 3:18, Yuanfang Chen @.***>) escribió:

I think the test case should be accepted.

When picking the overloaded non-template function, constraints partial ordering applied by https://eel.is/c++draft/over.match.best#general-2.6

— Reply to this email directly, view it on GitHub https://github.com/llvm/llvm-project/issues/58165#issuecomment-1278351489, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARCVZBG4DYCQBCOUEGCY7LTWDCYGPANCNFSM6AAAAAAQ5TVVOM . You are receiving this because you authored the thread.Message ID: @.***>

PiliLatiesa avatar Jan 28 '23 10:01 PiliLatiesa

This has been fixed since clang 16.

https://godbolt.org/z/3vj5zP1ne

mizvekov avatar May 02 '24 18:05 mizvekov