assert: Support types without operator== (int) [BZ #21972]
[platform/upstream/glibc.git] / assert / tst-assert-c++.cc
1 /* Tests for interactions between C++ and assert.
2    Copyright (C) 2017 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <assert.h>
20
21 /* The C++ standard requires that if the assert argument is a constant
22    subexpression, then the assert itself is one, too.  */
23 constexpr int
24 check_constexpr ()
25 {
26   return (assert (true), 1);
27 }
28
29 /* Objects of this class can be contextually converted to bool, but
30    cannot be compared to int.  */
31 struct no_int
32 {
33   no_int () = default;
34   no_int (const no_int &) = delete;
35
36   explicit operator bool () const
37   {
38     return true;
39   }
40
41   bool operator! () const; /* No definition.  */
42   template <class T> bool operator== (T) const; /* No definition.  */
43   template <class T> bool operator!= (T) const; /* No definition.  */
44 };
45
46 /* This class tests that operator== is not used by assert.  */
47 struct bool_and_int
48 {
49   bool_and_int () = default;
50   bool_and_int (const no_int &) = delete;
51
52   explicit operator bool () const
53   {
54     return true;
55   }
56
57   bool operator! () const; /* No definition.  */
58   template <class T> bool operator== (T) const; /* No definition.  */
59   template <class T> bool operator!= (T) const; /* No definition.  */
60 };
61
62 static int
63 do_test ()
64 {
65   {
66     no_int value;
67     assert (value);
68   }
69
70   {
71     bool_and_int value;
72     assert (value);
73   }
74
75   return 0;
76 }
77
78 #include <support/test-driver.c>