fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct TaskInterface {
  6. virtual void Do() = 0;
  7. virtual ~TaskInterface() {}
  8. };
  9.  
  10. template<class Impl>
  11. class TaskBase
  12. : public TaskInterface {
  13. virtual void Do() {
  14. DoDerivedImpl();
  15. }
  16. private:
  17. void DoDerivedImpl() {
  18. static_cast<Impl*>(this)->DoImpl();
  19. }
  20.  
  21. void DoImpl() {
  22. // Issue a static_assert error here, that there's no appropriate overridden
  23. // implementation of DoImpl() available:
  24. static_assert(static_cast<Impl*>(this)->DoImpl != TaskBase<Impl>::DoImpl, "TaskBase requires an appropriate implementation of DoImpl()");
  25. }
  26. };
  27.  
  28. class TaskType1 : public TaskBase<TaskType1> {
  29. public:
  30. void DoImpl() {
  31. cout << "TaskType1::DoImpl()" << endl;
  32. }
  33. };
  34.  
  35. class TaskType2 : public TaskBase<TaskType2> {
  36. public:
  37. void DoImpl() {
  38. cout << "TaskType2::DoImpl()" << endl;
  39. }
  40. };
  41.  
  42. class TaskType3 : public TaskBase<TaskType3> {
  43. };
  44.  
  45. int main() {
  46. std::vector<TaskInterface*> tasks;
  47. TaskType1 t1;
  48. TaskType2 t2;
  49. TaskType3 t3;
  50. tasks.push_back(&t1);
  51. tasks.push_back(&t2);
  52. tasks.push_back(&t3);
  53.  
  54. for(std::vector<TaskInterface*>::iterator it = tasks.begin();
  55. it != tasks.end();
  56. ++it) {
  57. (*it)->Do();
  58. }
  59. }
Compilation error #stdin compilation error #stdout 0s 3432KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void TaskBase<Impl>::DoImpl() [with Impl = TaskType3]’:
prog.cpp:18:13:   required from ‘void TaskBase<Impl>::DoDerivedImpl() [with Impl = TaskType3]’
prog.cpp:14:27:   required from ‘void TaskBase<Impl>::Do() [with Impl = TaskType3]’
prog.cpp:59:2:   required from here
prog.cpp:24:61: error: invalid use of member function (did you forget the ‘()’ ?)
              static_assert(static_cast<Impl*>(this)->DoImpl != TaskBase<Impl>::DoImpl, "TaskBase requires an appropriate implementation of DoImpl()");
                                                             ^
prog.cpp:24:61: error: invalid use of member function (did you forget the ‘()’ ?)
stdout
Standard output is empty