dkormalev / cefal

(Concepts-enabled) Functional Abstraction Layer for C++
BSD 3-Clause "New" or "Revised" License
53 stars 5 forks source link

Constraints vs static_assert #2

Open brevzin opened 4 years ago

brevzin commented 4 years ago

(This is not necessarily an issue).

The way that the various type classes are implemented all look basically like this:

template <typename T1, typename T2>
static std::optional<M> append(T1&& left, T2&& right) {
    static_assert(std::is_same_v<std::remove_cvref_t<T1>, std::optional<M>>, "Argument type should be the same as monoid");
    static_assert(std::is_same_v<std::remove_cvref_t<T2>, std::optional<M>>, "Argument type should be the same as monoid");

Is there a reason you chose unconstrained arguments with a static_assert, as opposed to an implementation strategy like:

template <DecaysTo<std::optional<M>> T1, DecaysTo<std::optional<M>> T2>
static std::optional<M> append(T1&& left, T2&& right) {
    // no static_asserts
}

The former might give you better diagnostics for direct invocations, the latter lets you "bubble up" the constraint to catch it earlier in the call stack.

dkormalev commented 4 years ago

Great idea, thank you! I think I like your suggestion better than current one (even if current one gives better diagnostics) just because it is cleaner and shorter. I'll definitely try to adopt it.