Last active
July 9, 2026 17:08
-
-
Save Eczbek/f5040181b2afb1bce6a5f8a043a54edc to your computer and use it in GitHub Desktop.
public_cast
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
| #include <cstddef> | |
| #include <meta> | |
| #include <string_view> | |
| #include <utility> | |
| #define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__) | |
| #define ARROW(...) \ | |
| noexcept(noexcept(__VA_ARGS__)) \ | |
| -> decltype(auto) \ | |
| requires(requires { __VA_ARGS__; }) \ | |
| { return (__VA_ARGS__); } | |
| #define LIFT(...) ([&](auto&&... _args) ARROW(__VA_ARGS__(FWD(_args)...))) | |
| namespace detail { | |
| struct fixed_string { | |
| const char* _data; | |
| std::size_t _size; | |
| template<std::size_t size> | |
| consteval fixed_string(const char (&data)[size]) | |
| : _data(std::define_static_string(data)), _size(size - 1) {} | |
| constexpr operator std::string_view() const { | |
| return std::string_view(this->_data, this->_size); | |
| } | |
| }; | |
| template<detail::fixed_string name, typename Args> | |
| struct public_cast_call { | |
| Args _args; | |
| friend constexpr decltype(auto) operator->*(auto&& obj, public_cast_call<name, Args> self) { | |
| template for (constexpr auto member : std::define_static_array(members_of(remove_cvref(^^decltype(obj)), std::meta::access_context::unchecked()))) { | |
| if constexpr (has_identifier(member) && (identifier_of(member) == name)) { | |
| auto&& [...args] = self._args; | |
| return (FWD(obj).*&[:member:])(FWD(args)...); | |
| } | |
| } | |
| std::unreachable(); | |
| } | |
| }; | |
| template<detail::fixed_string name> | |
| struct public_cast { | |
| friend constexpr decltype(auto) operator->*(auto&& obj, public_cast<name>) { | |
| template for (constexpr auto member : std::define_static_array(members_of(remove_cvref(^^decltype(obj)), std::meta::access_context::unchecked()))) { | |
| if constexpr (has_identifier(member) && (identifier_of(member) == name)) { | |
| return FWD(obj).*&[:member:]; | |
| } | |
| } | |
| std::unreachable(); | |
| } | |
| constexpr auto operator()(auto&&... args) const { | |
| struct Args; | |
| consteval { | |
| define_aggregate(^^Args, { | |
| data_member_spec(^^decltype(args)&&, { .name = "_" })... | |
| }); | |
| } | |
| return public_cast_call<name, Args>({ FWD(args)... }); | |
| } | |
| }; | |
| } | |
| template<detail::fixed_string name> | |
| constexpr auto public_cast = detail::public_cast<name>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment