Browse Source

meta: some compile-time checks to avoid subtle runtime errors

Michele Caini 4 years ago
parent
commit
530507c36b
1 changed files with 9 additions and 5 deletions
  1. 9 5
      src/entt/meta/factory.hpp

+ 9 - 5
src/entt/meta/factory.hpp

@@ -236,7 +236,7 @@ public:
      */
     template<typename Base>
     auto base() ENTT_NOEXCEPT {
-        static_assert(std::is_base_of_v<Base, Type>, "Invalid base type");
+        static_assert(!std::is_same_v<Type, Base> && std::is_base_of_v<Base, Type>, "Invalid base type");
 
         static internal::meta_base_node node{
             nullptr,
@@ -269,9 +269,12 @@ public:
      */
     template<auto Candidate>
     auto conv() ENTT_NOEXCEPT {
+        using conv_type = std::remove_const_t<std::remove_reference_t<std::invoke_result_t<decltype(Candidate), Type &>>>;
+        static_assert(!std::is_same_v<Type, conv_type> && std::is_convertible_v<Type, conv_type>, "Could not convert to the required type");
+
         static internal::meta_conv_node node{
             nullptr,
-            internal::meta_node<std::remove_const_t<std::remove_reference_t<std::invoke_result_t<decltype(Candidate), Type &>>>>::resolve(),
+            internal::meta_node<conv_type>::resolve(),
             [](const meta_any &instance) -> meta_any {
                 return forward_as_meta(std::invoke(Candidate, *static_cast<const Type *>(instance.data())));
             }
@@ -293,11 +296,12 @@ public:
      */
     template<typename To>
     auto conv() ENTT_NOEXCEPT {
-        static_assert(std::is_convertible_v<Type, To>, "Could not convert to the required type");
+        using conv_type = std::remove_const_t<std::remove_reference_t<To>>;
+        static_assert(!std::is_same_v<Type, conv_type> && std::is_convertible_v<Type, conv_type>, "Could not convert to the required type");
 
         static internal::meta_conv_node node{
             nullptr,
-            internal::meta_node<std::remove_const_t<std::remove_reference_t<To>>>::resolve(),
+            internal::meta_node<conv_type>::resolve(),
             [](const meta_any &instance) -> meta_any { return forward_as_meta(static_cast<To>(*static_cast<const Type *>(instance.data()))); }
             // tricks clang-format
         };
@@ -323,7 +327,7 @@ public:
     auto ctor() ENTT_NOEXCEPT {
         using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
         static_assert(Policy::template value<typename descriptor::return_type>, "Invalid return type for the given policy");
-        static_assert(std::is_same_v<std::decay_t<typename descriptor::return_type>, Type>, "The function doesn't return an object of the required type");
+        static_assert(std::is_same_v<std::remove_const_t<std::remove_reference_t<typename descriptor::return_type>>, Type>, "The function doesn't return an object of the required type");
 
         static internal::meta_ctor_node node{
             nullptr,