-
-
Save klenze/5bd6745fc65d4dda46ac328148ae1acd to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <functional> | |
#include <vector> | |
#include <cstdint> | |
struct Base | |
{ | |
std::vector<std::function<void(void)>> fOnInit{}; | |
Base() | |
{ | |
std::cout << "Calling Base constructor\n"; | |
} | |
void onInit() | |
{ | |
for (auto& f: fOnInit) | |
f(); | |
} | |
}; | |
// return a lambda expression whose return type is the class generated | |
#define MKCLASS(NAME) \ | |
[]() \ | |
{ \ | |
struct temp : public virtual Base \ | |
{ \ | |
uint32_t f ## NAME[64*256]; \ | |
temp() \ | |
{ \ | |
\ | |
fOnInit.push_back([this]() \ | |
{ \ | |
std::cout << "Setting up f"#NAME" at "<< &(this->f##NAME) \ | |
<<"\n"; \ | |
}); \ | |
} \ | |
} tempobj; \ | |
return tempobj; \ | |
} | |
template<auto... bases> | |
struct All: public decltype(bases())... | |
{ | |
}; | |
int main() | |
{ | |
All<MKCLASS(Foo), MKCLASS(Bar), MKCLASS(Baz)> a; | |
a.onInit(); | |
std::cout << &(a.fFoo) << "\n"; | |
std::cout << &(a.fBar) << "\n"; | |
std::cout << &(a.fBaz) << "\n"; | |
std::cout << sizeof(a.fOnInit) << "\n"; | |
std::cout << sizeof(a)*1.0/4/64/256 << "\n"; | |
} | |
/* | |
output | |
Calling Base constructor | |
Setting up fFoo at 0x7ffdc1a96958 | |
Setting up fBar at 0x7ffdc1aa6960 | |
Setting up fBaz at 0x7ffdc1ab6968 | |
0x7ffdc1a96958 | |
0x7ffdc1aa6960 | |
0x7ffdc1ab6968 | |
24 | |
3.00073 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment