【发布时间】:2022-11-28 00:29:47
【问题描述】:
我正在使用两个 constexpr std::array:
constexpr std::array full = { 1,2,3 };
constexpr std::array subset = { 3 };
如果第二个是第一个的子集,我想静态断言。
在上面的例子中,断言应该成功,而在下面的例子中,它应该失败:
constexpr std::array full = { 1,2,3 };
constexpr std::array subset = { 3, 4 };
我写了一个有效的 constexpr 函数:
constexpr bool isSubset(decltype(full)& a, decltype(subset)& b)
{
for (const auto& subsetElement : b) {
bool found = false;
for (const auto& fullElement : a) {
if (subsetElement == fullElement) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
但我想知道是否有另一种更直接(没有 decltype,也许没有 constexpr 函数?)的方法(最新的 CPP)。
【问题讨论】:
-
看起来你需要的是
ranges::includes。
标签: c++ constexpr constexpr-function