Created
July 29, 2025 20:15
-
-
Save hzhangxyz/8de55e7f244ae5f8f1bdaa388acb95c4 to your computer and use it in GitHub Desktop.
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 <format> | |
| #include <print> | |
| #include <string> | |
| #include <vector> | |
| struct Dog { | |
| std::string name; | |
| void Say() const { | |
| std::print("{}: Woof!\n", name); | |
| } | |
| }; | |
| struct Cat { | |
| std::string name; | |
| void Say() const { | |
| std::print("{}: Meow!\n", name); | |
| } | |
| }; | |
| struct MemberSayDecl { | |
| void Say() const; | |
| }; | |
| struct MemberSayImpl { | |
| void* value; | |
| void (*SayImpl)(void*); | |
| void Say() const { | |
| SayImpl(value); | |
| } | |
| }; | |
| struct MemberSayFatPtr { | |
| template<typename T> | |
| MemberSayFatPtr(T* v) : | |
| impl{ | |
| reinterpret_cast<void*>(v), | |
| [](void* self) { | |
| auto obj = static_cast<T*>(self); | |
| obj->Say(); | |
| }, | |
| } { } | |
| MemberSayImpl* operator->() { | |
| return &impl; | |
| } | |
| MemberSayImpl operator*() { | |
| return impl; | |
| } | |
| private: | |
| MemberSayImpl impl; | |
| }; | |
| int main() { | |
| auto c = Cat("a"); | |
| auto d = Dog("d"); | |
| auto p = std::vector<MemberSayFatPtr>{&c, &d}; | |
| p[0]->Say(); | |
| p[1]->Say(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题:
1、c++26不支持编译期动态类型,只支持编译期动态结构体,GG。。。
2、const的member function需要区分