c++ - 如果一个类只有一个通过 require 启用的成员函数,它仍然可以被模棱两可地重载吗?

标签 c++ c++-concepts

例如,这段代码有效吗?

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,但它似乎应该对我有用。

最佳答案

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

关于c++ - 如果一个类只有一个通过 require 启用的成员函数,它仍然可以被模棱两可地重载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43793941/

相关文章:

c++ - 如何定义返回 bool 的函数的概念

要求存在精确函数签名的 C++20 概念

c++ - 为什么 GLM 构造函数都是显式的?

c++ - 使用 N-API 将数据流式传输到 Node.js C++ 插件

C++ 2a - 多态范围

c++ - 模板函数中的参数类型可以推断吗?

C++:多态容器/迭代器与编译时概念/特征

c++ - 在不使用 std::bind 的情况下获取成员函数的 c++11 函数包装器

c++ - 在类中组织函数

c++ - shared_ptr 的隐式转换