相关疑难解决方法(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
查看次数

为什么same_as概念两次检查类型相等性?

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相同UU一样T?这不是多余的吗?

c++ concept c++20

17
推荐指数
2
解决办法
287
查看次数

标签 统计

c++ ×2

c++20 ×2

c++-concepts ×1

concept ×1