1 // is_instance_of_test.cpp -- The Boost Lambda Library ------------------
2 //
3 // Copyright (C) 2000-2003 Jaakko Jarvi ([email protected])
4 // Copyright (C) 2000-2003 Gary Powell ([email protected])
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // For more information, see www.boost.org
11
12 // -----------------------------------------------------------------------
13
14
15 #include <boost/core/lightweight_test.hpp>
16 #include <boost/core/lightweight_test_trait.hpp>
17
18
19 #include "boost/lambda/detail/is_instance_of.hpp"
20
21 #include <iostream>
22
23 template <class T1> struct A1 {};
24 template <class T1, class T2> struct A2 {};
25 template <class T1, class T2, class T3> struct A3 {};
26 template <class T1, class T2, class T3, class T4> struct A4 {};
27
28 class B1 : public A1<int> {};
29 class B2 : public A2<int,int> {};
30 class B3 : public A3<int,int,int> {};
31 class B4 : public A4<int,int,int,int> {};
32
33 // classes that are convertible to classes that derive from A instances
34 // This is not enough to make the test succeed
35
operator A1<int>()36 class C1 { public: operator A1<int>() { return A1<int>(); } };
operator B2()37 class C2 { public: operator B2() { return B2(); } };
operator B3()38 class C3 { public: operator B3() { return B3(); } };
operator B4()39 class C4 { public: operator B4() { return B4(); } };
40
41 // test that the result is really a constant
42 // (in an alternative implementation, gcc 3.0.2. claimed that it was
43 // a non-constant)
44 template <bool b> class X {};
45 // this should compile
46 X<boost::lambda::is_instance_of_2<int, A2>::value> x;
47
48
main()49 int main()
50 {
51 using boost::lambda::is_instance_of_1;
52 using boost::lambda::is_instance_of_2;
53 using boost::lambda::is_instance_of_3;
54 using boost::lambda::is_instance_of_4;
55
56
57 BOOST_TEST_TRAIT_TRUE((is_instance_of_1<B1, A1>));
58 BOOST_TEST_TRAIT_TRUE((is_instance_of_1<A1<float>, A1>));
59 BOOST_TEST_TRAIT_FALSE((is_instance_of_1<int, A1>));
60 BOOST_TEST_TRAIT_FALSE((is_instance_of_1<C1, A1>));
61
62 BOOST_TEST_TRAIT_TRUE((is_instance_of_2<B2, A2>));
63 BOOST_TEST_TRAIT_TRUE((is_instance_of_2<A2<int, float>, A2>));
64 BOOST_TEST_TRAIT_FALSE((is_instance_of_2<int, A2>));
65 BOOST_TEST_TRAIT_FALSE((is_instance_of_2<C2, A2>));
66
67 BOOST_TEST_TRAIT_TRUE((is_instance_of_3<B3, A3>));
68 BOOST_TEST_TRAIT_TRUE((is_instance_of_3<A3<int, float, char>, A3>));
69 BOOST_TEST_TRAIT_FALSE((is_instance_of_3<int, A3>));
70 BOOST_TEST_TRAIT_FALSE((is_instance_of_3<C3, A3>));
71
72 BOOST_TEST_TRAIT_TRUE((is_instance_of_4<B4, A4>));
73 BOOST_TEST_TRAIT_TRUE((is_instance_of_4<A4<int, float, char, double>, A4>));
74 BOOST_TEST_TRAIT_FALSE((is_instance_of_4<int, A4>));
75 BOOST_TEST_TRAIT_FALSE((is_instance_of_4<C4, A4>));
76
77 return boost::report_errors();
78 }
79