fork(1) download
  1. #include <cstdint>
  2.  
  3. // Seveval structs which should fail depending if T::f is virtual or not.
  4. template <typename T> struct Dvf : T { void f() final; };
  5. template <typename T> struct Dvo : T { void f() override; };
  6. template <typename T> struct Dnv : T { void f() = delete; };
  7.  
  8. template <typename U>
  9. class has_virtual_f
  10. {
  11. private:
  12. template <std::size_t N> struct helper {};
  13. template <typename T>
  14. static std::uint8_t check(helper<sizeof(Dvf<T>)>*);
  15. template<typename T> static std::uint16_t check(...);
  16. public:
  17. static
  18. constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t);
  19. };
  20.  
  21. // Sample struct.
  22. struct V { virtual void f(); };
  23. struct NV { void f(); };
  24. struct E { };
  25. struct F { virtual void f() final; }; // Bonus (unspecified expected output)
  26.  
  27. static_assert( has_virtual_f< V>::value, "");
  28. static_assert(!has_virtual_f<NV>::value, "");
  29. static_assert(!has_virtual_f< E>::value, "");
  30. //static_assert(/* ! */ has_virtual_f< F>::value, ""); (unspecified expected output)
  31.  
  32. int main() {
  33. return 0;
  34. }
  35.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘struct Dvf<NV>’:
prog.cpp:14:44:   required by substitution of ‘template<class T> static uint8_t has_virtual_f<U>::check(has_virtual_f<U>::helper<sizeof (Dvf<T>)>*) [with T = T; U = NV] [with T = NV]’
prog.cpp:18:40:   required from ‘constexpr const bool has_virtual_f<NV>::value’
prog.cpp:28:35:   required from here
prog.cpp:4:45: error: ‘void Dvf<T>::f() [with T = NV]’ marked final, but is not virtual
 template <typename T> struct Dvf : T { void f() final;    };
                                             ^
prog.cpp:28:1: error: static assertion failed: 
 static_assert(!has_virtual_f<NV>::value, "");
 ^
prog.cpp: In instantiation of ‘struct Dvf<E>’:
prog.cpp:14:44:   required by substitution of ‘template<class T> static uint8_t has_virtual_f<U>::check(has_virtual_f<U>::helper<sizeof (Dvf<T>)>*) [with T = T; U = E] [with T = E]’
prog.cpp:18:40:   required from ‘constexpr const bool has_virtual_f<E>::value’
prog.cpp:29:35:   required from here
prog.cpp:4:45: error: ‘void Dvf<T>::f() [with T = E]’ marked final, but is not virtual
 template <typename T> struct Dvf : T { void f() final;    };
                                             ^
prog.cpp:29:1: error: static assertion failed: 
 static_assert(!has_virtual_f< E>::value, "");
 ^
stdout
Standard output is empty