fork download
  1. #include <type_traits>
  2. #include <string>
  3.  
  4. template <typename Heir>
  5. class SToString {
  6. public:
  7. std::string toString() {
  8. return static_cast<Heir*>(this)->toStringImpl();
  9. }
  10. };
  11.  
  12. class Foo : public SToString<Foo> {
  13. public:
  14. std::string toStringImplRRRRRRRRRRRRR() {
  15. return "Foo";
  16. }
  17. };
  18.  
  19. int main() {
  20. Foo f;
  21.  
  22. f.toString();
  23.  
  24. return 0;
  25. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘std::string SToString<Heir>::toString() [with Heir = Foo; std::string = std::basic_string<char>]’:
prog.cpp:22:13:   required from here
prog.cpp:8:55: error: ‘class Foo’ has no member named ‘toStringImpl’
         return static_cast<Heir*>(this)->toStringImpl();
                                                       ^
prog.cpp: In member function ‘std::string SToString<Heir>::toString() [with Heir = Foo; std::string = std::basic_string<char>]’:
prog.cpp:9:5: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
stdout
Standard output is empty