protected_error.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 a public function contract cannot override from a protected one.
  6. #include <boost/contract/public_function.hpp>
  7. #include <boost/contract/base_types.hpp>
  8. #include <boost/contract/override.hpp>
  9. #include <boost/contract/check.hpp>
  10. struct b {
  11. protected:
  12. virtual void f(boost::contract::virtual_* v = 0) {
  13. boost::contract::check c = boost::contract::public_function(v, this);
  14. }
  15. friend class boost::contract::access; // Test this cannot prevent error.
  16. };
  17. struct a
  18. #define BASES public b
  19. : BASES
  20. {
  21. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  22. #undef BASES
  23. void f(boost::contract::virtual_* v = 0) /* override */ {
  24. boost::contract::check c = boost::contract::public_function<override_f>(
  25. v, &a::f, this); // Error (override cannot access b::f).
  26. #ifdef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  27. #error "Forcing error even when public functions not checked"
  28. #endif
  29. }
  30. // Correctly, GCC and CLang cannot even see b::f as part of overloaded bases
  31. // because it is protected. MSVC also fails compilation but only when
  32. // override_f below tries to call b::f (because it is protected so it cannot
  33. // be seen from within override_f).
  34. BOOST_CONTRACT_OVERRIDE(f)
  35. friend class boost::contract::access; // Test this cannot prevent error.
  36. };
  37. int main() {
  38. a aa;
  39. aa.f();
  40. return 0;
  41. }