Ami*_*rsh 4 c++ sfinae overload-resolution c++-concepts c++20
试图向 SFINAE 说再见。
是否可以使用concepts来区分函数,以便编译器可以根据发送的参数是否满足concept约束来匹配正确的函数?
例如,重载这两个:
// (a)
void doSomething(auto t) { /* */ }
// (b)
void doSomething(ConceptA auto t) { /* */ }
Run Code Online (Sandbox Code Playgroud)
因此,当被调用时,编译器会在每次调用时匹配正确的函数:
doSomething(param_doesnt_adhere_to_ConceptA); // calls (a)
doSomething(param_adheres_to_ConceptA); // calls (b)
Run Code Online (Sandbox Code Playgroud)
Yesconcepts是为此目的而设计的。如果发送的参数不符合所需的概念参数,则该函数不会被考虑在重载解析列表中,从而避免歧义。
此外,如果发送的参数满足多个功能,则会选择更具体的一个。
简单的例子:
void print(auto t) {
std::cout << t << std::endl;
}
void print(std::integral auto i) {
std::cout << "integral: " << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
以上print函数是可以一起存在的有效重载。
例如,调用函数:
print("hello"); // calls print(auto)
print(7); // calls print(std::integral auto)
Run Code Online (Sandbox Code Playgroud)
没有歧义——这两个功能可以完美地并排在一起。
不需要任何 SFINAE 代码,例如enable_if- 它已经应用(很好地隐藏)。
上面的例子展示了编译器如何比无约束类型(只是 auto)更喜欢受约束的类型(std::integral auto)。但这些规则也适用于两个相互竞争的概念。如果一个更具体,编译器应该选择更具体的一个。当然,如果这两个概念都得到满足并且没有一个更具体,这将导致歧义。
那么,是什么让一个概念更加具体呢?如果它基于另一个1。
通用概念 - GenericTwople:
template<class P>
concept GenericTwople = requires(P p) {
requires std::tuple_size<P>::value == 2;
std::get<0>(p);
std::get<1>(p);
};
Run Code Online (Sandbox Code Playgroud)
更具体的概念 - Twople:
class Any;
template<class Me, class TestAgainst>
concept type_matches =
std::same_as<TestAgainst, Any> ||
std::same_as<Me, TestAgainst> ||
std::derived_from<Me, TestAgainst>;
template<class P, class First, class Second>
concept Twople =
GenericTwople<P> && // <= note this line
type_matches<std::tuple_element_t<0, P>, First> &&
type_matches<std::tuple_element_t<1, P>, Second>;
Run Code Online (Sandbox Code Playgroud)
请注意,Twople 需要满足 GenericTwople 要求,因此它更具体。
如果您在我们的 Twople 中替换该行:
GenericTwople<P> && // <= note this line
Run Code Online (Sandbox Code Playgroud)
根据此行带来的实际需求,Twople 仍将具有相同的需求,但不再比 GenericTwople 更具体。当然,这与代码重用是我们更喜欢基于 GenericTwople 定义 Twople 的原因。
现在我们可以玩各种重载:
void print(auto t) {
cout << t << endl;
}
void print(const GenericTwople auto& p) {
cout << "GenericTwople: " << std::get<0>(p) << ", " << std::get<1>(p) << endl;
}
void print(const Twople<int, int> auto& p) {
cout << "{int, int}: " << std::get<0>(p) << ", " << std::get<1>(p) << endl;
}
Run Code Online (Sandbox Code Playgroud)
并调用它:
print(std::tuple{1, 2}); // goes to print(Twople<int, int>)
print(std::tuple{1, "two"}); // goes to print(GenericTwople)
print(std::pair{"three", 4}); // goes to print(GenericTwople)
print(std::array{5, 6}); // goes to print(Twople<int, int>)
print("hello"); // goes to print(auto)
Run Code Online (Sandbox Code Playgroud)
我们可以更进一步,因为上面介绍的 Twople 概念也适用于多态:
struct A{
virtual ~A() = default;
virtual std::ostream& print(std::ostream& out = std::cout) const {
return out << "A";
}
friend std::ostream& operator<<(std::ostream& out, const A& a) {
return a.print(out);
}
};
struct B: A{
std::ostream& print(std::ostream& out = std::cout) const override {
return out << "B";
}
};
Run Code Online (Sandbox Code Playgroud)
添加以下重载:
void print(const Twople<A, A> auto& p) {
cout << "{A, A}: " << std::get<0>(p) << ", " << std::get<1>(p) << endl;
}
Run Code Online (Sandbox Code Playgroud)
并调用它(而所有其他重载仍然存在):
print(std::pair{B{}, A{}}); // calls the specific print(Twople<A, A>)
Run Code Online (Sandbox Code Playgroud)
代码:https : //godbolt.org/z/3-O1Gz
不幸的是,C++20 不允许概念专门化,否则我们会走得更远,使用:
template<class P>
concept Twople<P, Any, Any> = GenericTwople<P>;
Run Code Online (Sandbox Code Playgroud)
这可以为这个 SO question添加一个很好的可能答案,但是不允许概念专业化。
1 Partial Ordering of Constraints 的实际规则比较复杂,参见:cppreference / C++20 spec。
| 归档时间: |
|
| 查看次数: |
267 次 |
| 最近记录: |