• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2008-2018 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0 (see accompanying
4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
6 
7 // Test a public function contract cannot override from a protected one.
8 
9 #include <boost/contract/public_function.hpp>
10 #include <boost/contract/base_types.hpp>
11 #include <boost/contract/override.hpp>
12 #include <boost/contract/check.hpp>
13 
14 struct b {
15 protected:
fb16     virtual void f(boost::contract::virtual_* v = 0) {
17         boost::contract::check c = boost::contract::public_function(v, this);
18     }
19 
20     friend class boost::contract::access; // Test this cannot prevent error.
21 };
22 
23 struct a
24     #define BASES public b
25     : BASES
26 {
27     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
28     #undef BASES
29 
fa30     void f(boost::contract::virtual_* v = 0) /* override */ {
31         boost::contract::check c = boost::contract::public_function<override_f>(
32                 v, &a::f, this); // Error (override cannot access b::f).
33         #ifdef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
34             #error "Forcing error even when public functions not checked"
35         #endif
36     }
37 
38     // Correctly, GCC and CLang cannot even see b::f as part of overloaded bases
39     // because it is protected. MSVC also fails compilation but only when
40     // override_f below tries to call b::f (because it is protected so it cannot
41     // be seen from within override_f).
42     BOOST_CONTRACT_OVERRIDE(f)
43 
44     friend class boost::contract::access; // Test this cannot prevent error.
45 };
46 
main()47 int main() {
48     a aa;
49     aa.f();
50     return 0;
51 }
52 
53