meta.cpp 7.2 KB

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