Skip to content

Instantly share code, notes, and snippets.

@dridk
Created September 1, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dridk/d879f976c5f51f611d2d41581297cb3c to your computer and use it in GitHub Desktop.
Save dridk/d879f976c5f51f611d2d41581297cb3c to your computer and use it in GitHub Desktop.
CRTP inheritance with static fields
#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