考虑这个例子:
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>?
在https://en.cppreference.com/w/cpp/concepts/same_as上查看same_as概念的可能实现,因为我注意到正在发生奇怪的事情。
namespace detail {
template< class T, class U >
concept SameHelper = std::is_same_v<T, U>;
}
template< class T, class U >
concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;
Run Code Online (Sandbox Code Playgroud)
第一个问题是为什么要插入一个SameHelper概念?第二个就是same_as检查是否T相同U和U一样T?这不是多余的吗?