Skip to content

Instantly share code, notes, and snippets.

@bkietz
Created June 23, 2023 13:08
Show Gist options
  • Save bkietz/4169272b1c18900979ed526e75bfe02c to your computer and use it in GitHub Desktop.
Save bkietz/4169272b1c18900979ed526e75bfe02c to your computer and use it in GitHub Desktop.
Minimal C++17 utility for writing `concept`-like checks
#pragma once
#include <type_traits>
/// Check can be used to determine whether expressions would be valid.
/// (Wraps SFINAE in as little boilerplate as possible).
/// Declare a Check like:
///
/// Check constexpr has_a = [](auto x) -> decltype(x.a) {};
///
/// After which it can be used like:
///
/// struct { int a; } does;
/// static_assert(has_a(does));
///
/// struct { int b; } does_not;
/// static_assert(not has_a(does_not));
///
/// To write a check which requires that an expression be truthy in addition to being valid:
///
/// Check constexpr value_3 = [](auto x) -> decltype(requires_<value(x) == 3>) {};
///
/// To write a check which requires multiple expressions be valid, separate them
/// with commas inside the `decltype()`:
///
/// Check constexpr has_a_and_b = [](auto x) -> decltype(x.a, x.b) {};
///
/// To write a check which accepts multiple branches, construct the Check with an
/// initializer list:
///
/// Check constexpr has_a_or_b{
/// [](auto x) -> decltype(x.a) {},
/// [](auto x) -> decltype(x.b) {},
/// };
///
/// If you don't want to declare the Check as a constexpr variable for some
/// reason, you can use it as a temporary instead:
///
/// int i;
/// static_assert(Check([](auto x) -> decltype(x + 1) {})(i));
///
/// Probably *don't* use this for conditions in enable_if. If you find yourself
/// trying to do this, probably you want to just use decltype() directly:
///
/// - template <typename T>
/// - enable_if_t<has_a(declval<T>()), // require that T has data member a
/// - T> frobnicate_a(T);
/// + template <typename T, typename DisableUnlessHasA = decltype(&T::a)>
/// + T frobnicate_a(T t);
///
/// Loosely inspired by Boost.Hana's is_valid().
template <typename... Fn>
struct Check {
constexpr Check(Fn...) {} // NOLINT(google-explicit-constructor)
template <typename... Args>
constexpr std::bool_constant<(... or std::is_invocable_v<Fn, Args &&...>)> operator()(
Args &&...) const {
return {};
}
};
template <bool Condition, typename = std::enable_if_t<Condition>>
static constexpr bool requires_ = true;
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment