fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. template<typename T, typename Setter>
  5. class Option
  6. {
  7. public:
  8. Option()
  9. : setter_(Setter)
  10. {
  11. }
  12.  
  13. /* operator= to be done, ruft dann setter mit value_ als value-Parameter auf */
  14. /* noch irgendwas, um intValue_ oder doubleValue_ direkt verwenden zu können */
  15.  
  16. private:
  17. T value_;
  18. std::function<bool(const T &source, T &value)> setter_;
  19. };
  20.  
  21. class SomeClass
  22. {
  23. public:
  24. void Blub()
  25. {
  26. intValue_ = 20;
  27. doubleValue_ = 4.2;
  28. }
  29.  
  30. void HighlyFrequentedFunction()
  31. {
  32. std::cout << "Result is: " << (intValue_ * doubleValue_) << "\n";
  33. }
  34.  
  35. private:
  36. Option<int, [] (const int &source, int &value) -> bool {
  37. value = source;
  38. return true;
  39. }> intValue_;
  40.  
  41. Option<double, [] (const double &source, double &value) -> bool {
  42. if (source < 2.0 || source > 5.0) return false;
  43. value = source * 2;
  44. return true;
  45. }> doubleValue_;
  46. };
  47.  
  48. int mai(int argc, char *argv[])
  49. {
  50. SomeClass test;
  51. test.Blub();
  52.  
  53. for (int i = 0; i < 20; ++i) test.HighlyFrequentedFunction();
  54.  
  55. return 0;
  56. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor ‘Option<T, Setter>::Option()’:
prog.cpp:9:19: error: expected primary-expression before ‘)’ token
prog.cpp: At global scope:
prog.cpp:39:3: error: type/value mismatch at argument 2 in template parameter list for ‘template<class T, class Setter> class Option’
prog.cpp:39:3: error:   expected a type, got ‘<lambda closure object><lambda(const int&, int&)>{}’
prog.cpp:45:3: error: template argument 2 is invalid
stdout
Standard output is empty