Created
September 1, 2018 17:55
-
-
Save dridk/d879f976c5f51f611d2d41581297cb3c to your computer and use it in GitHub Desktop.
CRTP inheritance with static fields
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 <map> | |
#include <string> | |
#include <list> | |
using namespace std; | |
// =========== Classe de base: caché a l'utilisateur ================================== | |
template <typename T> | |
class Base | |
{ | |
public: | |
void save() { | |
for (auto & i : metadatas) | |
cout<<i.first<<" "<<i.second<<" "<<endl; | |
}; | |
list<T> all() {}; | |
static map<string, int> metadatas; | |
}; | |
#define REGISTER_FIELDS(ClassName,...) template<> map<string, int> Base<ClassName>::metadatas = {__VA_ARGS__}; | |
// L'utilisateur créer des class ==================================================== | |
class User: public Base<User> | |
{ | |
}; | |
REGISTER_FIELDS (User, {"AGE",32},{"TRUC",23}) | |
class Comment: public Base<Comment> | |
{ | |
}; | |
REGISTER_FIELDS (Comment, {"POINT",12},{"NOTE",23}) | |
int main(int argc, char **argv) | |
{ | |
User user; | |
user.save(); | |
Comment comm; | |
comm.save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment