Bläddra i källkod

core: [[nodiscard]] (see #501)

Michele Caini 5 år sedan
förälder
incheckning
be5547de53

+ 12 - 12
src/entt/core/hashed_string.hpp

@@ -71,7 +71,7 @@ class basic_hashed_string {
     };
 
     // Fowler–Noll–Vo hash function v. 1a - the good
-    static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
+    [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
         auto value = traits_type::offset;
 
         while(*curr != 0) {
@@ -103,7 +103,7 @@ public:
      * @return The numeric representation of the string.
      */
     template<std::size_t N>
-    static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
+    [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
         return helper(str);
     }
 
@@ -112,7 +112,7 @@ public:
      * @param wrapper Helps achieving the purpose by relying on overloading.
      * @return The numeric representation of the string.
      */
-    static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
+    [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
         return helper(wrapper.str);
     }
 
@@ -122,7 +122,7 @@ public:
      * @param size Length of the string to hash.
      * @return The numeric representation of the string.
      */
-    static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
+    [[nodiscard]] static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
         id_type partial{traits_type::offset};
         while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
         return partial;
@@ -165,7 +165,7 @@ public:
      * @brief Returns the human-readable representation of a hashed string.
      * @return The string used to initialize the instance.
      */
-    constexpr const value_type * data() const ENTT_NOEXCEPT {
+    [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
         return str;
     }
 
@@ -173,25 +173,25 @@ public:
      * @brief Returns the numeric representation of a hashed string.
      * @return The numeric representation of the instance.
      */
-    constexpr hash_type value() const ENTT_NOEXCEPT {
+    [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
         return hash;
     }
 
     /*! @copydoc data */
-    constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
+    [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
 
     /**
      * @brief Returns the numeric representation of a hashed string.
      * @return The numeric representation of the instance.
      */
-    constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
+    [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
 
     /**
      * @brief Compares two hashed strings.
      * @param other Hashed string with which to compare.
      * @return True if the two hashed strings are identical, false otherwise.
      */
-    constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
+    [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
         return hash == other.hash;
     }
 
@@ -224,7 +224,7 @@ basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
  * @return True if the two hashed strings are identical, false otherwise.
  */
 template<typename Char>
-constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
+[[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
     return !(lhs == rhs);
 }
 
@@ -245,7 +245,7 @@ using hashed_wstring = basic_hashed_string<wchar_t>;
  * @param str The literal without its suffix.
  * @return A properly initialized hashed string.
  */
-constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
+[[nodiscard]] constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
     return entt::hashed_string{str};
 }
 
@@ -255,7 +255,7 @@ constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::si
  * @param str The literal without its suffix.
  * @return A properly initialized hashed wstring.
  */
-constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
+[[nodiscard]] constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
     return entt::hashed_wstring{str};
 }
 

+ 1 - 1
src/entt/core/ident.hpp

@@ -45,7 +45,7 @@ class identifier {
     using tuple_type = std::tuple<std::decay_t<Types>...>;
 
     template<typename Type, std::size_t... Indexes>
-    static constexpr id_type get(std::index_sequence<Indexes...>) {
+    [[nodiscard]] static constexpr id_type get(std::index_sequence<Indexes...>) {
         static_assert(std::disjunction_v<std::is_same<Type, Types>...>, "Invalid type");
         return (0 + ... + (std::is_same_v<Type, std::tuple_element_t<Indexes, tuple_type>> ? id_type(Indexes) : id_type{}));
     }

+ 1 - 1
src/entt/core/monostate.hpp

@@ -38,7 +38,7 @@ struct monostate {
      * @return Stored value, if any.
      */
     template<typename Type>
-    operator Type() const ENTT_NOEXCEPT {
+    [[nodiscard]] operator Type() const ENTT_NOEXCEPT {
         return value<Type>;
     }
 

+ 9 - 9
src/entt/core/type_info.hpp

@@ -22,7 +22,7 @@ namespace internal {
 
 
 struct ENTT_API type_index {
-    static id_type next() ENTT_NOEXCEPT {
+    [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
         static ENTT_MAYBE_ATOMIC(id_type) value{};
         return value++;
     }
@@ -30,7 +30,7 @@ struct ENTT_API type_index {
 
 
 template<typename Type>
-constexpr auto type_name() ENTT_NOEXCEPT {
+[[nodiscard]] constexpr auto type_name() ENTT_NOEXCEPT {
 #if defined ENTT_PRETTY_FUNCTION
     std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
     auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
@@ -61,7 +61,7 @@ struct ENTT_API type_index {
      * @brief Returns the sequential identifier of a given type.
      * @return The sequential identifier of a given type.
      */
-    static id_type value() ENTT_NOEXCEPT {
+    [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
         static const id_type value = internal::type_index::next();
         return value;
     }
@@ -101,17 +101,17 @@ struct type_info {
      * @return The numeric representation of the given type.
      */
 #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
-    static constexpr id_type id() ENTT_NOEXCEPT {
+    [[nodiscard]] static constexpr id_type id() ENTT_NOEXCEPT {
         constexpr auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION);
         return value;
     }
 #elif defined ENTT_PRETTY_FUNCTION
-    static id_type id() ENTT_NOEXCEPT {
+    [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
         static const auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION);
         return value;
     }
 #else
-    static id_type id() ENTT_NOEXCEPT {
+    [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
         return type_index<Type>::value();
     }
 #endif
@@ -121,17 +121,17 @@ struct type_info {
      * @return The name of the given type.
      */
 #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
-    static constexpr std::string_view name() ENTT_NOEXCEPT {
+    [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
         constexpr auto value = internal::type_name<Type>();
         return value;
     }
 #elif defined ENTT_PRETTY_FUNCTION
-    static std::string_view name() ENTT_NOEXCEPT {
+    [[nodiscard]] static std::string_view name() ENTT_NOEXCEPT {
         static const auto value = internal::type_name<Type>();
         return value;
     }
 #else
-    static constexpr std::string_view name() ENTT_NOEXCEPT {
+    [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
         return internal::type_name<Type>();
     }
 #endif

+ 1 - 2
src/entt/core/type_traits.hpp

@@ -6,7 +6,6 @@
 #include <utility>
 #include <type_traits>
 #include "../config/config.h"
-#include "hashed_string.hpp"
 #include "fwd.hpp"
 
 
@@ -235,7 +234,7 @@ using member_class_t = typename member_class<Member>::type;
      * @param id The value to convert.\
      * @return The integral representation of the given value.
      */\
-    constexpr auto to_integral(const clazz id) ENTT_NOEXCEPT {\
+    [[nodiscard]] constexpr auto to_integral(const clazz id) ENTT_NOEXCEPT {\
         return static_cast<std::underlying_type_t<clazz>>(id);\
     }\
     static_assert(true)

+ 3 - 3
src/entt/core/utility.hpp

@@ -18,7 +18,7 @@ struct identity {
      * @return The submitted value as-is.
      */
     template<class Type>
-    constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
+    [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
         return std::forward<Type>(value);
     }
 };
@@ -32,7 +32,7 @@ struct identity {
  * @return Pointer to the member.
  */
 template<typename Type, typename Class>
-constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
+[[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
 
 
 /**
@@ -42,7 +42,7 @@ constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  * @return Pointer to the function.
  */
 template<typename Func>
-constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
+[[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
 
 
 /**