fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. template <typename ... Types> struct GenKey;
  6. template <typename R, typename ... Types> struct GenKey <R, Types...> : public GenKey<Types...>{
  7. template <typename P>
  8. constexpr bool operator()(const P&p) const { return is_same<R,P>::value || GenKey<Types...>::operator()(p); }
  9. };
  10. template <typename R> struct GenKey <R>{
  11. template <typename P>
  12. constexpr bool operator()(const P&) const { return is_same<R,P>::value; }
  13. };
  14. template <> struct GenKey <>{
  15. template <typename P>
  16. constexpr bool operator()(const P&) const { return false; }
  17. };
  18.  
  19. class Server {
  20. public:
  21. struct common{};
  22. struct canEdit{};
  23. struct canKill{};
  24. public:
  25. Server(){}
  26. void exec(const auto& key) {
  27. if (key(common())) {
  28. execCommon();
  29. }
  30. if (key(canEdit())) {
  31. execCanEdit();
  32. }
  33. if (key(canKill())) {
  34. execCanKill();
  35. }
  36. }
  37. private:
  38. void execCommon() {
  39. cout << "common exec" << endl;
  40. }
  41. void execCanEdit() {
  42. cout << "edit exec" << endl;
  43. }
  44. void execCanKill() {
  45. cout << "kill exec" << endl;
  46. }
  47. };
  48.  
  49. int main() {
  50. Server server;
  51. cout << "key0" << endl;
  52. server.exec(GenKey<>());
  53. cout << "key1" << endl;
  54. server.exec(GenKey<Server::common,Server::canEdit>());
  55. return 0;
  56. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
key0
key1
common exec
edit exec