xref: /aosp_15_r20/external/eigen/test/meta.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <[email protected]>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 template<typename From, typename To>
check_is_convertible(const From &,const To &)13 bool check_is_convertible(const From&, const To&)
14 {
15   return internal::is_convertible<From,To>::value;
16 }
17 
18 struct FooReturnType {
19   typedef int ReturnType;
20 };
21 
22 struct MyInterface {
23   virtual void func() = 0;
~MyInterfaceMyInterface24   virtual ~MyInterface() {}
25 };
26 struct MyImpl : public MyInterface {
funcMyImpl27   void func() {}
28 };
29 
EIGEN_DECLARE_TEST(meta)30 EIGEN_DECLARE_TEST(meta)
31 {
32   VERIFY((internal::conditional<(3<4),internal::true_type, internal::false_type>::type::value));
33   VERIFY(( internal::is_same<float,float>::value));
34   VERIFY((!internal::is_same<float,double>::value));
35   VERIFY((!internal::is_same<float,float&>::value));
36   VERIFY((!internal::is_same<float,const float&>::value));
37 
38   VERIFY(( internal::is_same<float,internal::remove_all<const float&>::type >::value));
39   VERIFY(( internal::is_same<float,internal::remove_all<const float*>::type >::value));
40   VERIFY(( internal::is_same<float,internal::remove_all<const float*&>::type >::value));
41   VERIFY(( internal::is_same<float,internal::remove_all<float**>::type >::value));
42   VERIFY(( internal::is_same<float,internal::remove_all<float**&>::type >::value));
43   VERIFY(( internal::is_same<float,internal::remove_all<float* const *&>::type >::value));
44   VERIFY(( internal::is_same<float,internal::remove_all<float* const>::type >::value));
45 
46   // test add_const
47   VERIFY(( internal::is_same< internal::add_const<float>::type, const float >::value));
48   VERIFY(( internal::is_same< internal::add_const<float*>::type, float* const>::value));
49   VERIFY(( internal::is_same< internal::add_const<float const*>::type, float const* const>::value));
50   VERIFY(( internal::is_same< internal::add_const<float&>::type, float& >::value));
51 
52   // test remove_const
53   VERIFY(( internal::is_same< internal::remove_const<float const* const>::type, float const* >::value));
54   VERIFY(( internal::is_same< internal::remove_const<float const*>::type, float const* >::value));
55   VERIFY(( internal::is_same< internal::remove_const<float* const>::type, float* >::value));
56 
57   // test add_const_on_value_type
58   VERIFY(( internal::is_same< internal::add_const_on_value_type<float&>::type, float const& >::value));
59   VERIFY(( internal::is_same< internal::add_const_on_value_type<float*>::type, float const* >::value));
60 
61   VERIFY(( internal::is_same< internal::add_const_on_value_type<float>::type, const float >::value));
62   VERIFY(( internal::is_same< internal::add_const_on_value_type<const float>::type, const float >::value));
63 
64   VERIFY(( internal::is_same< internal::add_const_on_value_type<const float* const>::type, const float* const>::value));
65   VERIFY(( internal::is_same< internal::add_const_on_value_type<float* const>::type, const float* const>::value));
66 
67   VERIFY(( internal::is_same<float,internal::remove_reference<float&>::type >::value));
68   VERIFY(( internal::is_same<const float,internal::remove_reference<const float&>::type >::value));
69   VERIFY(( internal::is_same<float,internal::remove_pointer<float*>::type >::value));
70   VERIFY(( internal::is_same<const float,internal::remove_pointer<const float*>::type >::value));
71   VERIFY(( internal::is_same<float,internal::remove_pointer<float* const >::type >::value));
72 
73 
74   // is_convertible
75   STATIC_CHECK(( internal::is_convertible<float,double>::value ));
76   STATIC_CHECK(( internal::is_convertible<int,double>::value ));
77   STATIC_CHECK(( internal::is_convertible<int, short>::value ));
78   STATIC_CHECK(( internal::is_convertible<short, int>::value ));
79   STATIC_CHECK(( internal::is_convertible<double,int>::value ));
80   STATIC_CHECK(( internal::is_convertible<double,std::complex<double> >::value ));
81   STATIC_CHECK((!internal::is_convertible<std::complex<double>,double>::value ));
82   STATIC_CHECK(( internal::is_convertible<Array33f,Matrix3f>::value ));
83   STATIC_CHECK(( internal::is_convertible<Matrix3f&,Matrix3f>::value ));
84   STATIC_CHECK(( internal::is_convertible<Matrix3f&,Matrix3f&>::value ));
85   STATIC_CHECK(( internal::is_convertible<Matrix3f&,const Matrix3f&>::value ));
86   STATIC_CHECK(( internal::is_convertible<const Matrix3f&,Matrix3f>::value ));
87   STATIC_CHECK(( internal::is_convertible<const Matrix3f&,const Matrix3f&>::value ));
88   STATIC_CHECK((!internal::is_convertible<const Matrix3f&,Matrix3f&>::value ));
89   STATIC_CHECK((!internal::is_convertible<const Matrix3f,Matrix3f&>::value ));
90   STATIC_CHECK(!( internal::is_convertible<Matrix3f,Matrix3f&>::value ));
91 
92   STATIC_CHECK(!( internal::is_convertible<int,int&>::value ));
93   STATIC_CHECK(( internal::is_convertible<const int,const int& >::value ));
94 
95   //STATIC_CHECK((!internal::is_convertible<Matrix3f,Matrix3d>::value )); //does not even compile because the conversion is prevented by a static assertion
96   STATIC_CHECK((!internal::is_convertible<Array33f,int>::value ));
97   STATIC_CHECK((!internal::is_convertible<MatrixXf,float>::value ));
98   {
99     float f = 0.0f;
100     MatrixXf A, B;
101     VectorXf a, b;
102     VERIFY(( check_is_convertible(a.dot(b), f) ));
103     VERIFY(( check_is_convertible(a.transpose()*b, f) ));
104     VERIFY((!check_is_convertible(A*B, f) ));
105     VERIFY(( check_is_convertible(A*B, A) ));
106   }
107 
108   #if (EIGEN_COMP_GNUC  && EIGEN_COMP_GNUC  <=  99) \
109    || (EIGEN_COMP_CLANG && EIGEN_COMP_CLANG <= 909) \
110    || (EIGEN_COMP_MSVC  && EIGEN_COMP_MSVC  <=1914)
111   // See http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1752,
112   // basically, a fix in the c++ standard breaks our c++98 implementation
113   // of is_convertible for abstract classes.
114   // So the following tests are expected to fail with recent compilers.
115 
116   STATIC_CHECK(( !internal::is_convertible<MyInterface, MyImpl>::value ));
117   #if (!EIGEN_COMP_GNUC_STRICT) || (EIGEN_GNUC_AT_LEAST(4,8))
118   // GCC prior to 4.8 fails to compile this test:
119   // error: cannot allocate an object of abstract type 'MyInterface'
120   // In other word, it does not obey SFINAE.
121   // Nevertheless, we don't really care about supporting abstract type as scalar type!
122   STATIC_CHECK(( !internal::is_convertible<MyImpl, MyInterface>::value ));
123   #endif
124   STATIC_CHECK((  internal::is_convertible<MyImpl, const MyInterface&>::value ));
125 
126   #endif
127 
128   {
129     int i = 0;
130     VERIFY(( check_is_convertible(fix<3>(), i) ));
131     VERIFY((!check_is_convertible(i, fix<DynamicIndex>()) ));
132   }
133 
134 
135   VERIFY((  internal::has_ReturnType<FooReturnType>::value ));
136   VERIFY((  internal::has_ReturnType<ScalarBinaryOpTraits<int,int> >::value ));
137   VERIFY(( !internal::has_ReturnType<MatrixXf>::value ));
138   VERIFY(( !internal::has_ReturnType<int>::value ));
139 
140   VERIFY(internal::meta_sqrt<1>::ret == 1);
141   #define VERIFY_META_SQRT(X) VERIFY(internal::meta_sqrt<X>::ret == int(std::sqrt(double(X))))
142   VERIFY_META_SQRT(2);
143   VERIFY_META_SQRT(3);
144   VERIFY_META_SQRT(4);
145   VERIFY_META_SQRT(5);
146   VERIFY_META_SQRT(6);
147   VERIFY_META_SQRT(8);
148   VERIFY_META_SQRT(9);
149   VERIFY_META_SQRT(15);
150   VERIFY_META_SQRT(16);
151   VERIFY_META_SQRT(17);
152   VERIFY_META_SQRT(255);
153   VERIFY_META_SQRT(256);
154   VERIFY_META_SQRT(257);
155   VERIFY_META_SQRT(1023);
156   VERIFY_META_SQRT(1024);
157   VERIFY_META_SQRT(1025);
158 }
159