考虑这个例子:
template <typename T> inline constexpr bool C1 = true;
template <typename T> inline constexpr bool C2 = true;
template <typename T> requires C1<T> && C2<T>
constexpr int foo() { return 0; }
template <typename T> requires C1<T>
constexpr int foo() { return 1; }
constexpr int bar() {
return foo<int>();
}
Run Code Online (Sandbox Code Playgroud)
调用是foo<int>()
不明确的,还是约束C1<T> && C2<T>
包含C1<T>
?
我刚刚了解到 C++20 提供了三种将约束应用于模板的方法:
我成功地使用了受约束的模板参数,而且我更喜欢这种语法。我仍然想知道为什么标准中定义了三种方式。
是否存在根本的技术差异?这些风味是否有独特的优点或缺点(除了语法外观)?