xref: /petsc/include/petsc/private/cpp/crtp.hpp (revision 9dd11ecf0918283bb567d8b33a92f53ac4ea7840)
1 #pragma once
2 
3 namespace Petsc
4 {
5 
6 namespace util
7 {
8 
9 // A useful crtp helper class to abstract away all the static_cast<Derived *>(this) nonsense
10 template <template <typename, typename...> class CRTPType, typename Derived, typename... T>
11 class crtp {
12 protected:
underlying()13   Derived       &underlying() noexcept { return static_cast<Derived &>(*this); }
underlying() const14   const Derived &underlying() const noexcept { return static_cast<const Derived &>(*this); }
15 
16 private:
17   // private constructor + friend decl preempts any diamond dependency problems
18   // https://www.fluentcpp.com/2017/05/19/crtp-helper/
19   constexpr crtp() noexcept = default;
20   friend CRTPType<Derived, T...>;
21 };
22 
23 } // namespace util
24 
25 } // namespace Petsc
26