I'm using two constexpr std::array:
constexpr std::array full = { 1,2,3 };
constexpr std::array subset = { 3 };
I would like to static assert if the second is a subset of the first.
In the above example, the assertion should succeed, and in the following, it supposes to fail:
constexpr std::array full = { 1,2,3 };
constexpr std::array subset = { 3, 4 };
I wrote a working constexpr function:
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;
}
But I wondered if there is another, more straightforward (without decltype, maybe without constexpr function?) way of doing it (latest CPP).
Rather than a nest for-loop, or sorting both array, you can also just sort the longer one, and loop on the other one while performing a binary search: