type_mismatch_error.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2008-2018 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. // Test override public function error on result type mismatch.
  6. #include <boost/contract/public_function.hpp>
  7. #include <boost/contract/override.hpp>
  8. #include <boost/contract/base_types.hpp>
  9. #include <boost/contract/check.hpp>
  10. struct b {
  11. virtual int f(boost::contract::virtual_* v = 0) {
  12. // Unfortunately, this cannot be made to error at compile-time because
  13. // in this case public_function does not that &b::f as param (but this
  14. // will error at run-time on a virtual call via a derived class).
  15. char result;
  16. boost::contract::check c = boost::contract::public_function(
  17. v, result, this);
  18. return result;
  19. }
  20. };
  21. struct a
  22. #define BASES public b
  23. : BASES
  24. {
  25. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  26. #undef BASES
  27. virtual int f(boost::contract::virtual_* v = 0) /* override */ {
  28. char result;
  29. boost::contract::check c = boost::contract::public_function<override_f>(
  30. v, result, &a::f, this); // Error (result time mismatch).
  31. #ifdef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  32. #error "Forcing error even when public functions not checked"
  33. #endif
  34. return result;
  35. }
  36. BOOST_CONTRACT_OVERRIDE(f)
  37. };
  38. int main() {
  39. a aa;
  40. aa.f();
  41. return 0;
  42. }