gw  1.0.0
A bunch of small C++ utilities
Loading...
Searching...
No Matches
crtp.hpp
1// Copyright (c) 2023 Martin Stump
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <concepts>
7#include <type_traits>
8
10namespace gw {
11
13template <template <typename> class T, typename Derived>
14 requires std::is_class_v<Derived> && std::same_as<Derived, std::remove_cv_t<Derived>>
15struct crtp {
17 constexpr auto self() noexcept -> Derived& {
18 static_assert(std::derived_from<Derived, T<Derived>>);
19 return static_cast<Derived&>(*this);
20 }
21
23 constexpr auto self() const noexcept -> Derived const& {
24 static_assert(std::derived_from<Derived, T<Derived>>);
25 return static_cast<const Derived&>(*this);
26 }
27
29 constexpr auto operator<=>(const crtp&) const noexcept = default;
30
31 private:
32 crtp() = default;
33 friend T<Derived>;
34};
35
36} // namespace gw
GW namespace.
Definition concepts.hpp:14
CRTP base class.
Definition crtp.hpp:15
constexpr auto self() const noexcept -> Derived const &
Returns a reference to the derived class.
Definition crtp.hpp:23
constexpr auto self() noexcept -> Derived &
Returns a reference to the derived class.
Definition crtp.hpp:17