fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. struct LoggingConfig {};
  5.  
  6. template<typename Derived>
  7. class Logging {
  8. public:
  9. explicit Logging(const LoggingConfig& config) {}
  10.  
  11. static std::unique_ptr<Derived> CreateIfLoggingEnabled(const LoggingConfig& config) {
  12. return std::make_unique<Derived>(config);
  13. }
  14. };
  15.  
  16. class LoggingString : public Logging<LoggingString> {
  17. public:
  18. using Logging<LoggingString>::Logging;
  19.  
  20. void writeLog(std::string str) { std::cout << "[LoggingString] " << str << std::endl; }
  21. };
  22.  
  23. class LoggingInt : public Logging<LoggingInt> {
  24. public:
  25. using Logging<LoggingInt>::Logging;
  26.  
  27. void writeLog(int i) { std::cout << "[LoggingInt] " << i << std::endl; }
  28. };
  29.  
  30. int main()
  31. {
  32. auto logger1 = LoggingString::CreateIfLoggingEnabled(LoggingConfig{});
  33. logger1->writeLog("hello");
  34.  
  35. auto logger2 = LoggingInt::CreateIfLoggingEnabled(LoggingConfig{});
  36. logger2->writeLog(12345);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5520KB
stdin
Standard input is empty
stdout
[LoggingString] hello
[LoggingInt] 12345