Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions include/samurai/crtp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2025 the samurai's authors
// SPDX-License-Identifier: BSD-3-Clause

#pragma once

namespace samurai
{
template <class D>
class crtp_base
{
public:

using derived_type = D;

const derived_type& derived_cast() const& noexcept;
derived_type& derived_cast() & noexcept;
derived_type derived_cast() && noexcept;

protected:

crtp_base() = default;
~crtp_base() = default;

crtp_base(const crtp_base&) = default;
crtp_base& operator=(const crtp_base&) = default;

crtp_base(crtp_base&&) noexcept = default;
crtp_base& operator=(crtp_base&&) noexcept = default;
};

template <class D>
inline auto crtp_base<D>::derived_cast() const& noexcept -> const derived_type&
{
return *static_cast<const derived_type*>(this);
}

template <class D>
inline auto crtp_base<D>::derived_cast() & noexcept -> derived_type&
{
return *static_cast<derived_type*>(this);
}

template <class D>
inline auto crtp_base<D>::derived_cast() && noexcept -> derived_type
{
return *static_cast<derived_type*>(this);
}
}
Loading
Loading