例如,这段代码有效吗?
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/