C++ Logo

std-proposals

Advanced search

[std-proposals] DR: concepts std::strict_weak_ordering / std::equivalence_relation must be resticted by semantic requirements

From: Nikl Kelbon <kelbonage_at_[hidden]>
Date: Sat, 6 May 2023 18:12:41 +0400
Now (for compiler) std::strict_weak_ordering<...> is same as
std::relation<...>, and std::equivalence_relation is same as std::relation
This produces amgibuity:

#include <concepts>
#include <functional>

bool foo(std::strict_weak_order<int, int> auto x) { return true; }
bool foo(std::relation<int, int> auto x) { return false; }

int main() {
    foo(std::less{});
}

I propose create a two different implementation concepts
strict_weak_order_sematic_requirement<R, T, U>
and
equivalence_relation_semantic_requirement<R, T, U>

#include <concepts>
#include <functional>
// Must be different for equivalence_relation
template<typename...>
concept always_true = true;
template<typename T, typename A, typename B>
concept strict_weak_order_semantic_requirement =
always_true<T, A, B>;
template<typename T, typename A, typename B>
concept strict_weak_order = std::relation<T, A, B>
&& strict_weak_order_semantic_requirement<T, A, B>;

bool foo(strict_weak_order<int, int> auto x) { return true; }
bool foo(std::relation<int, int> auto x) { return false; }

int main() {
    foo(std::less{});
}

They must be different, such that strcit_weak_order !=
equivalence_relation, this solves ambiguity in overload resolution(
https://godbolt.org/z/n7ovccf9n) and explicitly in code declares our
intensions.
May be in future implementation of this semantic concepts will be possible.

Received on 2023-05-06 14:12:55