fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class FeaturesExample {};
  5. class Example : public FeaturesExample {};
  6.  
  7. template<typename D, typename T>
  8. class Base {
  9. public:
  10. virtual float getFloat(const T* ex) const = 0;
  11. float getFloat(const FeaturesExample& ex) const {
  12. get(reinterpret_cast<T*>(&ex));
  13. }
  14. float (Base::*getFloatImplPtr_)(const FeaturesExample& ex) const;
  15. float getFloatImpl_(const FeaturesExample& ex) const {
  16. return 0.0;
  17. }
  18. };
  19.  
  20. class Derived : public Base<Derived, Example> {
  21. public:
  22. virtual float getFloatImpl(const Example* ex) override {
  23. return 1.0;
  24. }
  25. };
  26. int main() {
  27. FeaturesExample ex;
  28. Derived derived;
  29. auto f = derived.getFloat(ex);
  30. std::cout << f << std::endl;
  31. return 0;
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:22:17: error: ‘virtual float Derived::getFloatImpl(const Example*)’ marked ‘override’, but does not override
   virtual float getFloatImpl(const Example* ex) override {
                 ^~~~~~~~~~~~
prog.cpp: In function ‘int main()’:
prog.cpp:28:10: error: cannot declare variable ‘derived’ to be of abstract type ‘Derived’
  Derived derived;
          ^~~~~~~
prog.cpp:20:7: note:   because the following virtual functions are pure within ‘Derived’:
 class Derived : public Base<Derived, Example> {
       ^~~~~~~
prog.cpp:10:17: note: 	float Base<D, T>::getFloat(const T*) const [with D = Derived; T = Example]
   virtual float getFloat(const T* ex) const = 0;
                 ^~~~~~~~
prog.cpp: In instantiation of ‘float Base<D, T>::getFloat(const FeaturesExample&) const [with D = Derived; T = Example]’:
prog.cpp:29:30:   required from here
prog.cpp:12:8: error: reinterpret_cast from type ‘const FeaturesExample*’ to type ‘Example*’ casts away qualifiers
    get(reinterpret_cast<T*>(&ex));
        ^~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:12:7: error: ‘get’ was not declared in this scope
    get(reinterpret_cast<T*>(&ex));
    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
stdout
Standard output is empty