fork(2) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class ConfigPair
  5. {
  6. protected:
  7. std::string name;
  8. public:
  9. ConfigPair(const std::string &n) : name(n) { }
  10. virtual std::string toString() const = 0;
  11. };
  12.  
  13. class ConfigPairInt : public ConfigPair
  14. {
  15. int value;
  16. public:
  17. ConfigPairInt(const std::string &n, int v) : ConfigPair(n), value(v) { }
  18. std::string toString() const override { return name + ":i[" + std::to_string(value) + "]"; }
  19. };
  20.  
  21. class ConfigPairDouble : public ConfigPair
  22. {
  23. double value;
  24. public:
  25. ConfigPairDouble(const std::string &n, double v) : ConfigPair(n), value(v) { }
  26. std::string toString() const override { return name + ":d[" + std::to_string(value) + "]"; }
  27. };
  28.  
  29. class ConfigPairString : public ConfigPair
  30. {
  31. std::string value;
  32. public:
  33. ConfigPairString(const std::string &n, const std::string &v) : ConfigPair(n), value(v) { }
  34. std::string toString() const override { return name + ":s[\"" + value + "\"]"; }
  35. };
  36.  
  37. struct Config
  38. {
  39. template<typename... Ts>
  40. Config(const Ts&... args)
  41. {
  42. const ConfigPair* arr[] = {&args...};
  43. // use args as needed...
  44. for (const ConfigPair* arg : arr) {
  45. std::cout << arg->toString() << std::endl;
  46. }
  47. }
  48. };
  49.  
  50. int main()
  51. {
  52. Config c2(
  53. ConfigPairInt("one", 1),
  54. ConfigPairDouble("two", 2.0),
  55. ConfigPairString("three", "3")
  56. );
  57. return 0;
  58. }
Success #stdin #stdout 0s 4376KB
stdin
Standard input is empty
stdout
one:i[1]
two:d[2.000000]
three:s["3"]