如果 class 只有一个通过 require 启用的成员函数,它仍然可以被模糊地重载吗?

If a class has only a single member function enabled via requires can it still be ambiguously overloaded?

例如,此代码是否有效?

template <class T>
struct A {
  void f() 
    requires std::is_same_v<T, int>
  {
  }

  void f(int) 
    requires !std::is_same_v<T, int>
  {
  }
};

int main() {
  auto fptr = &A<int>::f;
  return 0;
}

它不会 compile 与 gcc,但它似乎对我来说应该有用。

如果您尝试从问题行的代码中删除自动类型推导,编译器会给出更合适的消息:

prog.cc: In function 'int main()':
prog.cc:18:40: error: conversion from '<unresolved overloaded function 
 type>' to non-scalar type 'std::function<void()>' requested
   std::function<void()> fptr = A<int>::f;

尝试解析 A::f 有两个可能的选项,编译器如何知道您没有搞砸并打算调用另一个。

If a class has only a single member function enabled via requires is it still considered overloaded?

是。

[C++ Concepts TS: 13/1]: When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By extension, two declarations in the same scope that declare the same name but with different types or different associated constraints (14.10.2) are called overloaded declarations. Only function and function template declarations can be overloaded; variable and type declarations cannot be overloaded.

过载分辨率在它们之间选择:

[C++ Concepts TS: 13.3.2/1]: From the set of candidate functions constructed for a given context (13.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints for the best fit (13.3.3). The selection of viable functions considers associated constraints, if any (14.10.2), and relationships between arguments and function parameters other than the ranking of conversion sequences.


For example, is this code valid?

是的!

虽然这里有两个重载,但在取地址 f:

时不考虑约束不满足的重载

[C++ Concepts TS: 13.4/4]: Eliminate from the set of selected functions all those whose constraints are not satisfied [..]

因此,这似乎是一个编译器错误。

引用版本:N4377,日期 2015-02-09