Actual source code: crtp.hpp

      1: #pragma once

      3: namespace Petsc
      4: {

      6: namespace util
      7: {

      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:
     13:   Derived       &underlying() noexcept { return static_cast<Derived &>(*this); }
     14:   const Derived &underlying() const noexcept { return static_cast<const Derived &>(*this); }

     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: };

     23: } // namespace util

     25: } // namespace Petsc