# Exotic CRTP pattern for static polymorphism using variadic mixin composition and C++23 explicit object parameters.

> Source: <https://gist.github.com/unrays/4eae18cf6a6e46e83c071fdaa1d03205>
> Published: 2026-05-22 20:00:44+00:00

exotic_crtp.hpp

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      
Learn more about bidirectional Unicode characters

 
    Show hidden characters

// Copyright (c) May 2026 Félix-Olivier Dumas. All rights reserved.

// Licensed under the terms described in the LICENSE file

// Reference example of the pattern

// See: https://medium.com/@felixolivierdumas/exotic-crtp-rethinking-static-polymorphism-with-c-23-89f9e75e8ffd

#pragma once

#include <iostream>

#include <type_traits>

#include <utility>

namespace exotic {

    template<typename... From>

    struct crtp_access : From... {};

    template<typename T>

    constexpr decltype(auto) as_crtp(T&& obj) noexcept {

        using crtp_access_t = crtp_access<std::remove_cvref_t<T>>;

        return static_cast<crtp_access_t&&>(obj);

    }

}

struct Base {

    void interface(this auto&& self) {

        return exotic::as_crtp(self).implementation();

    }

};

struct Derived : Base {

    void implementation(this exotic::crtp_access<Derived> self) {

        std::cout << "Derived implementation" << std::endl;

    }

};

int main() {

    Derived d;

 

    d.interface(); // perfectly works

 

    //d.implementation(); -> doesn't work, Derived only shows .interface()

}
