相关疑难解决方法(0)

约束包含仅适用于概念吗?

考虑这个例子:

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++ c++-concepts c++20

17
推荐指数
1
解决办法
425
查看次数

将约束应用于模板的三种方式有什么区别?

我刚刚了解到 C++20 提供了三种将约束应用于模板的方法:

  • 需要条款
  • 尾随需要条款
  • 约束模板参数

我成功地使用了受约束的模板参数,而且我更喜欢这种语法。我仍然想知道为什么标准中定义了三种方式。

是否存在根本的技术差异?这些风味是否有独特的优点或缺点(除了语法外观)?

c++ c++-concepts c++20

4
推荐指数
1
解决办法
85
查看次数

标签 统计

c++ ×2

c++-concepts ×2

c++20 ×2