From 28d4423b6057a3a710c08c197918235f84637f6d Mon Sep 17 00:00:00 2001 From: scarf Date: Wed, 9 Oct 2024 01:57:06 +0900 Subject: [PATCH 1/2] refactor: apply `modernize-{use-constraints,type-traits}` --- src/all_enum_values.h | 5 ++- src/assign.h | 26 +++++++------- src/cata_utility.h | 4 +-- src/cata_variant.h | 15 ++++---- src/catalua_luna.h | 4 +-- src/coordinates.h | 12 +++---- src/dialogue.h | 4 +-- src/enum_bitset.h | 4 +-- src/enum_conversions.h | 2 +- src/enum_traits.h | 2 +- src/event.h | 2 +- src/fmtlib_core.h | 37 +++++++++++--------- src/fmtlib_format-inl.h | 4 +-- src/fmtlib_format.h | 34 +++++++++--------- src/fmtlib_ostream.h | 6 ++-- src/generic_readers.h | 4 +-- src/hash_utils.h | 6 ++-- src/int_id.h | 9 +++-- src/item.h | 2 +- src/json.h | 75 +++++++++++++++++++--------------------- src/mod_tracker.h | 10 +++--- src/output.h | 23 ++++++------ src/pimpl.h | 10 +++--- src/point_traits.h | 4 +-- src/ret_val.h | 6 ++-- src/rng.h | 2 +- src/simple_pathfinding.h | 3 +- src/stats_tracker.h | 2 +- src/string_formatter.h | 57 +++++++++++++++--------------- src/string_id.h | 12 +++---- src/string_id_utils.h | 13 ++++--- src/units_utility.h | 4 +-- 32 files changed, 202 insertions(+), 201 deletions(-) diff --git a/src/all_enum_values.h b/src/all_enum_values.h index 232a59fa3513..1eff87d5a6d0 100644 --- a/src/all_enum_values.h +++ b/src/all_enum_values.h @@ -2,6 +2,9 @@ #ifndef CATA_SRC_ALL_ENUM_VALUES_H #define CATA_SRC_ALL_ENUM_VALUES_H +#include +#include + #include "enum_traits.h" template @@ -24,4 +27,4 @@ auto all_enum_values() -> const std::array()> & return all_enum_values_helper( std::make_index_sequence()> {} ); } -#endif // CATA_SRC_ALL_ENUM_VALUES_H \ No newline at end of file +#endif // CATA_SRC_ALL_ENUM_VALUES_H diff --git a/src/assign.h b/src/assign.h index c4ba8e82e871..e5f2093145f4 100644 --- a/src/assign.h +++ b/src/assign.h @@ -32,7 +32,7 @@ class is_optional_helper> : public std::true_type }; } // namespace detail template -class is_optional : public detail::is_optional_helper::type> +class is_optional : public detail::is_optional_helper> { }; @@ -134,9 +134,9 @@ bool assign( const JsonObject &jo, const std::string &name, std::pair &val // Note: is_optional excludes any types based on std::optional, which is // handled below in a separate function. -template < typename T, typename std::enable_if < std::is_class::value &&!is_optional::value, - int >::type = 0 > +template < typename T> bool assign( const JsonObject &jo, const std::string &name, T &val, bool strict = false ) +requires( std::is_class_v && !is_optional::value ) //*NOPAD* { T out; if( !jo.read( name, out ) ) { @@ -363,18 +363,17 @@ bool assign( const JsonObject &jo, // Kinda hacky way to avoid allowing multiplying temperature // For example, in 10 * 0 Fahrenheit, 10 * 0 Celsius - what's the expected result of those? -template < typename lvt, typename ut, typename s, - typename std::enable_if_t::common_zero_point::value>* = nullptr> +template < typename lvt, typename ut, typename s> inline units::quantity mult_unit( const JsonObject &, const std::string &, const units::quantity &val, const s scalar ) -{ +requires units::quantity_details::common_zero_point::value { return val * scalar; } -template < typename lvt, typename ut, typename s, - typename std::enable_if_t < !units::quantity_details::common_zero_point::value > * = nullptr > +template < typename lvt, typename ut, typename s> inline units::quantity mult_unit( const JsonObject &err, const std::string &name, const units::quantity &, const s ) +requires( !units::quantity_details::common_zero_point::value ) { err.throw_error( "Multiplying units with multiple scales with different zero points is not well defined", name ); @@ -454,16 +453,17 @@ bool assign( const JsonObject &jo, class time_duration; template -inline typename -std::enable_if::type, time_duration>::value, bool>::type +inline bool read_with_factor( const JsonObject &jo, const std::string &name, T &val, const T &factor ) -{ +requires std::is_same_v, time_duration> { int tmp; - if( jo.read( name, tmp, false ) ) { + if( jo.read( name, tmp, false ) ) + { // JSON contained a raw number -> apply factor val = tmp * factor; return true; - } else if( jo.has_string( name ) ) { + } else if( jo.has_string( name ) ) + { // JSON contained a time duration string -> no factor val = read_from_json_string( *jo.get_raw( name ), time_duration::units ); return true; diff --git a/src/cata_utility.h b/src/cata_utility.h index 7e5ed5378e10..c4f68f1569b0 100644 --- a/src/cata_utility.h +++ b/src/cata_utility.h @@ -57,9 +57,9 @@ double round_up( double val, unsigned int dp ); * * @p num must be non-negative, @p den must be positive, and @c num+den must not overflow. */ -template::value, int>::type = 0> +template T divide_round_up( T num, T den ) -{ +requires std::is_integral_v { return ( num + den - 1 ) / den; } diff --git a/src/cata_variant.h b/src/cata_variant.h index 2910b59132ff..c35ebfc30bbc 100644 --- a/src/cata_variant.h +++ b/src/cata_variant.h @@ -95,7 +95,7 @@ constexpr cata_variant_type type_for_impl( std::index_sequence ) { constexpr size_t num_types = static_cast( cata_variant_type::num_types ); constexpr std::array matches = {{ - std::is_same( I )>::type>::value... + std::is_same_v( I )>::type>... } }; for( size_t i = 0; i < num_types; ++i ) { @@ -112,7 +112,7 @@ constexpr cata_variant_type type_for_impl( std::index_sequence ) template struct convert_string { using type = T; - static_assert( std::is_same::value, + static_assert( std::is_same_v, "Intended for use only with string typedefs" ); static std::string to_string( const T &v ) { return v; @@ -355,10 +355,9 @@ class cata_variant // Constructor that attempts to infer the type from the type of the // value passed. - template < typename Value, - typename = std::enable_if_t <( - cata_variant_type_for() < cata_variant_type::num_types )>> - explicit cata_variant( Value && value ) { + template < typename Value> + explicit cata_variant( Value &&value ) requires( + cata_variant_type_for() < cata_variant_type::num_types ) { constexpr cata_variant_type Type = cata_variant_type_for(); type_ = Type; value_ = @@ -372,8 +371,8 @@ class cata_variant template static cata_variant make( Value &&value ) { return cata_variant( - Type, cata_variant_detail::convert::to_string( - std::forward( value ) ) ); + Type, cata_variant_detail::convert::to_string( + std::forward( value ) ) ); } // Call this to construct from a type + string. This should rarely be diff --git a/src/catalua_luna.h b/src/catalua_luna.h index d1c0858a5938..a31ad5609d31 100644 --- a/src/catalua_luna.h +++ b/src/catalua_luna.h @@ -87,7 +87,7 @@ struct luna_traits { extern std::string_view current_comment; template -using remove_cv_ref_t = typename std::remove_cv::type>::type; +using remove_cv_ref_t = std::remove_cv_t>; template using fx_traits = sol::meta::meta_detail::fx_traits; @@ -107,7 +107,7 @@ std::string doc_value_impl() if constexpr( luna_traits::impl ) { return std::string( luna_traits::name ); } else { - using ValNoPtr = typename std::remove_pointer::type; + using ValNoPtr = std::remove_pointer_t; using ValBare = remove_cv_ref_t; if constexpr( luna_traits::impl ) { return std::string( luna_traits::name ); diff --git a/src/coordinates.h b/src/coordinates.h index eac2a372bdd5..25e73d8ea6e7 100644 --- a/src/coordinates.h +++ b/src/coordinates.h @@ -227,13 +227,11 @@ constexpr inline auto operator+( return coord_point( l.raw() + r.raw() ); } -template < typename PointL, typename PointR, origin OriginR, scale Scale, - // enable_if to prevent ambiguity with above when both args are - // relative - typename = std::enable_if_t < OriginR != origin::relative >> +template < typename PointL, typename PointR, origin OriginR, scale Scale> constexpr inline auto operator+( const coord_point &l, const coord_point &r ) +requires( OriginR != origin::relative ) { using PointResult = decltype( PointL() + PointR() ); return coord_point( l.raw() + r.raw() ); @@ -248,13 +246,11 @@ constexpr inline auto operator-( return coord_point( l.raw() - r.raw() ); } -template < typename PointL, typename PointR, origin Origin, scale Scale, - // enable_if to prevent ambiguity with above when both args are - // relative - typename = std::enable_if_t < Origin != origin::relative >> +template < typename PointL, typename PointR, origin Origin, scale Scale> constexpr inline auto operator-( const coord_point &l, const coord_point &r ) +requires( Origin != origin::relative ) { using PointResult = decltype( PointL() + PointR() ); return coord_point( l.raw() - r.raw() ); diff --git a/src/dialogue.h b/src/dialogue.h index 0ccfa43c22fc..c39fca5d300c 100644 --- a/src/dialogue.h +++ b/src/dialogue.h @@ -36,8 +36,8 @@ enum dialogue_consequence : unsigned char { action }; -using talkfunction_ptr = std::add_pointer::type; -using dialogue_fun_ptr = std::add_pointer::type; +using talkfunction_ptr = std::add_pointer_t; +using dialogue_fun_ptr = std::add_pointer_t; using trial_mod = std::pair; diff --git a/src/enum_bitset.h b/src/enum_bitset.h index 7b1dffbd9d90..0a4a7bcfb1d8 100644 --- a/src/enum_bitset.h +++ b/src/enum_bitset.h @@ -10,7 +10,7 @@ template class enum_bitset { - static_assert( std::is_enum::value, "the template argument is not an enum." ); + static_assert( std::is_enum_v, "the template argument is not an enum." ); static_assert( has_enum_traits::value, "a specialization of 'enum_traits' template containing 'last' element of the enum must be defined somewhere. " "The `last` constant must be of the same type as the enum iteslf." @@ -89,7 +89,7 @@ class enum_bitset private: static constexpr size_t get_pos( E e ) noexcept { - return static_cast( static_cast::type>( e ) ); + return static_cast( static_cast>( e ) ); } std::bitset::size()> bits; diff --git a/src/enum_conversions.h b/src/enum_conversions.h index 3e16d5dde4bc..589a58c94d32 100644 --- a/src/enum_conversions.h +++ b/src/enum_conversions.h @@ -41,7 +41,7 @@ std::string enum_to_string( E ); template std::unordered_map build_enum_lookup_map() { - static_assert( std::is_enum::value, "E should be an enum type" ); + static_assert( std::is_enum_v, "E should be an enum type" ); static_assert( has_enum_traits::value, "enum E needs a specialization of enum_traits" ); std::unordered_map result; diff --git a/src/enum_traits.h b/src/enum_traits.h index f89069b1619a..81b79504c353 100644 --- a/src/enum_traits.h +++ b/src/enum_traits.h @@ -11,7 +11,7 @@ namespace enum_traits_detail { template -using last_type = typename std::decay::last )>::type; +using last_type = std::decay_t::last )>; } // namespace enum_traits_detail diff --git a/src/event.h b/src/event.h index 43cc7b451902..36f599c002ce 100644 --- a/src/event.h +++ b/src/event.h @@ -560,7 +560,7 @@ class event using Spec = event_detail::event_spec; // Using is_empty mostly just to verify that the type is defined at // all, but it so happens that it ought to be empty too. - static_assert( std::is_empty::value, + static_assert( std::is_empty_v, "spec for this event type must be defined and empty" ); static_assert( sizeof...( Args ) == Spec::fields.size(), "wrong number of arguments for event type" ); diff --git a/src/fmtlib_core.h b/src/fmtlib_core.h index b6ad50ad7a9f..2fb5791c1390 100644 --- a/src/fmtlib_core.h +++ b/src/fmtlib_core.h @@ -257,16 +257,16 @@ FMT_BEGIN_NAMESPACE // Implementations of enable_if_t and other metafunctions for older systems. template -using enable_if_t = typename std::enable_if::type; +using enable_if_t = std::enable_if_t; template -using conditional_t = typename std::conditional::type; +using conditional_t = std::conditional_t; template using bool_constant = std::integral_constant; template -using remove_reference_t = typename std::remove_reference::type; +using remove_reference_t = std::remove_reference_t; template -using remove_const_t = typename std::remove_const::type; +using remove_const_t = std::remove_const_t; template -using remove_cvref_t = typename std::remove_cv>::type; +using remove_cvref_t = std::remove_cv_t>; template struct type_identity { using type = T; }; @@ -329,10 +329,10 @@ struct uint128_t {}; // Casts a nonnegative integer to unsigned. template -FMT_CONSTEXPR typename std::make_unsigned::type to_unsigned( Int value ) +FMT_CONSTEXPR std::make_unsigned_tto_unsigned( Int value ) { FMT_ASSERT( value >= 0, "negative value" ); - return static_cast::type>( value ); + return static_cast>( value ); } FMT_SUPPRESS_MSC_WARNING( 4566 ) constexpr unsigned char micro[] = "\u00B5"; @@ -1376,8 +1376,8 @@ template struct arg_mapper { !has_fallback_formatter::value ) > FMT_CONSTEXPR auto map( const T &val ) -> decltype( std::declval().map( - static_cast::type>( val ) ) ) { - return map( static_cast::type>( val ) ); + static_cast>( val ) ) ) { + return map( static_cast>( val ) ); } template < typename T, FMT_ENABLE_IF( !is_string::value @@ -1839,8 +1839,8 @@ inline auto make_args_checked( const S &format_str, { static_assert( detail::count < ( - std::is_base_of>::value && - std::is_reference::value )... > () == 0, + std::is_base_of_v> && + std::is_reference_v )... > () == 0, "passing views as lvalues is disallowed" ); detail::check_format_string( format_str ); return {args...}; @@ -1889,8 +1889,8 @@ class dynamic_format_arg_store enum { value = !( detail::is_reference_wrapper::value || - std::is_same>::value || - std::is_same>::value || + std::is_same_v> || + std::is_same_v> || ( mapped_type != detail::type::cstring_type && mapped_type != detail::type::string_type && mapped_type != detail::type::custom_type ) ) @@ -1993,7 +1993,7 @@ class dynamic_format_arg_store */ template void push_back( std::reference_wrapper arg ) { static_assert( - detail::is_named_arg::type>::value || + detail::is_named_arg>::value || need_copy::value, "objects of built-in types and string views are always copied" ); emplace_arg( arg.get() ); @@ -2215,7 +2215,8 @@ template , bool enable = detail::is_output_iterator::value> auto vformat_to( OutputIt out, const S &format_str, basic_format_args>> args ) --> typename std::enable_if::type +-> OutputIt + requires( enable ) { decltype( detail::get_buffer( out ) ) buf( detail::get_buffer_init( out ) ); detail::vformat_to( buf, to_string_view( format_str ), args ); @@ -2237,7 +2238,8 @@ auto vformat_to( OutputIt out, const S &format_str, template >::value> inline auto format_to( OutputIt out, const S &format_str, Args && ... args ) -> -typename std::enable_if::type +OutputIt +requires( enable ) { const auto &vargs = fmt::make_args_checked( format_str, args... ); return vformat_to( out, to_string_view( format_str ), vargs ); @@ -2273,7 +2275,8 @@ template >::value> inline auto format_to_n( OutputIt out, size_t n, const S &format_str, const Args &... args ) -> -typename std::enable_if>::type +format_to_n_result +requires( enable ) { const auto &vargs = fmt::make_args_checked( format_str, args... ); return vformat_to_n( out, n, to_string_view( format_str ), vargs ); diff --git a/src/fmtlib_format-inl.h b/src/fmtlib_format-inl.h index 86ab7fedc638..15dfdd69774c 100644 --- a/src/fmtlib_format-inl.h +++ b/src/fmtlib_format-inl.h @@ -2647,7 +2647,7 @@ void fallback_format( Double d, int num_digits, bool binary32, buffer &buf template int format_float( T value, int precision, float_specs specs, buffer &buf ) { - static_assert( !std::is_same::value, "" ); + static_assert( !std::is_same_v, "" ); FMT_ASSERT( value >= 0, "value is negative" ); const bool fixed = specs.format == float_format::fixed; @@ -2718,7 +2718,7 @@ int snprintf_float( T value, int precision, float_specs specs, { // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. FMT_ASSERT( buf.capacity() > buf.size(), "empty buffer" ); - static_assert( !std::is_same::value, "" ); + static_assert( !std::is_same_v, "" ); // Subtract 1 to account for the difference in precision since we use %e for // both general and exponent format. diff --git a/src/fmtlib_format.h b/src/fmtlib_format.h index 69939c7e4a30..09a08d0b5e1b 100644 --- a/src/fmtlib_format.h +++ b/src/fmtlib_format.h @@ -1,4 +1,5 @@ #pragma once +//NOLINT(modernize-type-traits) /* Formatting library for C++ @@ -641,9 +642,9 @@ inline size_t code_point_index( basic_string_view s, size_t n ) template using needs_conversion = bool_constant < - std::is_same::value_type, - char>::value && - std::is_same::value >; + std::is_same_v::value_type, + char> && + std::is_same_v >; template < typename OutChar, typename InputIt, typename OutputIt, FMT_ENABLE_IF( !needs_conversion::value ) > @@ -891,7 +892,7 @@ namespace detail template using is_signed = std::integral_constant < bool, std::numeric_limits::is_signed || - std::is_same::value >; + std::is_same_v >; // Returns true if value is negative, false otherwise. // Same as `value < 0` but doesn't produce warnings if T is an unsigned type. @@ -909,9 +910,9 @@ FMT_CONSTEXPR bool is_negative( T ) template ::value )> FMT_CONSTEXPR bool is_supported_floating_point( T ) { - return ( std::is_same::value && FMT_USE_FLOAT ) || - ( std::is_same::value && FMT_USE_DOUBLE ) || - ( std::is_same::value && FMT_USE_LONG_DOUBLE ); + return ( std::is_same_v &&FMT_USE_FLOAT ) || + ( std::is_same_v &&FMT_USE_DOUBLE ) || + ( std::is_same_v &&FMT_USE_LONG_DOUBLE ); } // Smallest of uint32_t, uint64_t, uint128_t that is large enough to @@ -2242,7 +2243,7 @@ OutputIt write( OutputIt out, T value ) return out; } - using floaty = conditional_t::value, double, T>; + using floaty = conditional_t, double, T>; using uint = typename dragonbox::float_info::carrier_uint; auto bits = bit_cast( value ); @@ -2397,10 +2398,9 @@ OutputIt write( OutputIt out, const void *value ) } template -auto write( OutputIt out, const T &value ) -> typename std::enable_if < -mapped_type_constant>::value == - type::custom_type, - OutputIt >::type +auto write( OutputIt out, const T &value ) -> OutputIt +requires( mapped_type_constant>::value == + type::custom_type ) { using context_type = basic_format_context; using formatter_type = @@ -2731,9 +2731,9 @@ template class custom_formatter template using is_integer = - bool_constant < is_integral::value && !std::is_same::value && - !std::is_same::value && - !std::is_same::value >; + bool_constant < is_integral::value && !std::is_same_v && + !std::is_same_v && + !std::is_same_v >; template class width_checker { @@ -3186,7 +3186,7 @@ constexpr Char to_ascii( Char value ) return value; } template ::value )> -constexpr typename std::underlying_type::type to_ascii( Char value ) +constexpr std::underlying_type_tto_ascii( Char value ) { return value; } @@ -4359,7 +4359,7 @@ make_format_to_n_args( const Args &... args ) return format_arg_store, Args...>( args... ); } -template < typename Char, enable_if_t < ( !std::is_same::value ), int >> +template < typename Char, enable_if_t < ( !std::is_same_v ), int >> std::basic_string detail::vformat( basic_string_view format_str, basic_format_args>> args ) diff --git a/src/fmtlib_ostream.h b/src/fmtlib_ostream.h index 43d3d9cff478..513bc620ee17 100644 --- a/src/fmtlib_ostream.h +++ b/src/fmtlib_ostream.h @@ -80,9 +80,9 @@ template class is_streamable { private: template - static bool_constant < !std::is_same < decltype( std::declval&>() + static bool_constant < !std::is_same_v < decltype( std::declval&>() << std::declval() ), - void_t< >>::value > + void_t< >> > test( int ); template static std::false_type test( ... ); @@ -98,7 +98,7 @@ template void write_buffer( std::basic_ostream &os, buffer &buf ) { const Char *buf_data = buf.data(); - using unsigned_streamsize = std::make_unsigned::type; + using unsigned_streamsize = std::make_unsigned_t; unsigned_streamsize size = buf.size(); unsigned_streamsize max_size = to_unsigned( max_value() ); do { diff --git a/src/generic_readers.h b/src/generic_readers.h index e7ac43a242e3..ffc230809716 100644 --- a/src/generic_readers.h +++ b/src/generic_readers.h @@ -208,9 +208,9 @@ class generic_typed_reader * The `enable_if` is here to prevent the compiler from considering it * when called on a simple data member, the other `operator()` will be used. */ - template::is_container, int>::type = 0> + template bool operator()( const JsonObject &jo, const std::string &member_name, - C &container, bool was_loaded ) const { + C &container, bool was_loaded ) const requires reader_detail::handler::is_container { const Derived &derived = static_cast( *this ); // If you get an error about "incomplete type 'struct reader_detail::handler...", // you have to implement a specialization of your container type, so above for diff --git a/src/hash_utils.h b/src/hash_utils.h index 1fa418ff5188..e965248abb83 100644 --- a/src/hash_utils.h +++ b/src/hash_utils.h @@ -91,7 +91,8 @@ namespace hash64_detail { template -std::enable_if_t < sizeof( T ) < 8, T > maybe_mix_bits( std::uint64_t val ) +T maybe_mix_bits( std::uint64_t val ) +requires( sizeof( T ) < 8 ) { std::uint32_t hi = val >> 32; std::uint32_t lo = val; @@ -101,7 +102,8 @@ std::enable_if_t < sizeof( T ) < 8, T > maybe_mix_bits( std::uint64_t val ) } template -std::enable_if_t < sizeof( T ) >= 8, T > maybe_mix_bits( std::uint64_t val ) +T maybe_mix_bits( std::uint64_t val ) +requires( sizeof( T ) >= 8 ) { return val; } diff --git a/src/int_id.h b/src/int_id.h index 9dde61fcbdff..3465c6b4d108 100644 --- a/src/int_id.h +++ b/src/int_id.h @@ -32,8 +32,8 @@ class int_id /** * Prevent accidental construction from other int ids. */ - template < typename S, typename std::enable_if_t < !std::is_same::value, int > = 0 > - int_id( const int_id &id ) = delete; + template < typename S> + int_id( const int_id &id ) requires( !std::is_same_v ) = delete; /** * Default constructor constructs a 0-id. No id value is special to this class, 0 as id @@ -53,9 +53,8 @@ class int_id * constructor to create the int id. This allows plain C-strings, * and std::strings to be used. */ - template::value>::type > - explicit int_id( S && id ) : int_id( string_id( std::forward( id ) ) ) {} + template requires std::is_convertible_v + explicit int_id( S &&id ) : int_id( string_id( std::forward( id ) ) ) {} /** * Comparison, only useful when the id is used in std::map or std::set as key. diff --git a/src/item.h b/src/item.h index d728fa337ef5..849ad3774055 100644 --- a/src/item.h +++ b/src/item.h @@ -170,7 +170,7 @@ struct iteminfo { inline iteminfo::flags operator|( iteminfo::flags l, iteminfo::flags r ) { - using I = std::underlying_type::type; + using I = std::underlying_type_t; return static_cast( static_cast( l ) | r ); } diff --git a/src/json.h b/src/json.h index be8457d2824b..8931446fbe39 100644 --- a/src/json.h +++ b/src/json.h @@ -71,7 +71,7 @@ struct key_from_json_string, void> { }; template -struct key_from_json_string::value>> { +struct key_from_json_string>> { Enum operator()( const std::string &s ) { return io::string_to_enum( s ); } @@ -231,8 +231,8 @@ class JsonIn JsonObject get_object(); JsonArray get_array(); - template::value>::type> - E get_enum_value() { + template + E get_enum_value() requires std::is_enum_v { const auto old_offset = tell(); try { return io::string_to_enum( get_string() ); @@ -334,8 +334,8 @@ class JsonIn } } - template::value, int> = 0> - bool read( T &val, bool throw_on_error = false ) { + template + bool read( T &val, bool throw_on_error = false ) requires std::is_enum_v { int i; if( read( i, false ) ) { val = static_cast( i ); @@ -422,10 +422,9 @@ class JsonIn } // array ~> vector, deque, list - template < typename T, typename std::enable_if < - !std::is_same::value >::type * = nullptr - > - auto read( T &v, bool throw_on_error = false ) -> decltype( v.front(), true ) { + template < typename T> + auto read( T &v, bool throw_on_error = false ) -> decltype( v.front(), + true ) requires( !std::is_same_v ) { if( !test_array() ) { return error_or_false( throw_on_error, "Expected json array" ); } @@ -510,10 +509,9 @@ class JsonIn // object ~> containers with matching key_type and value_type // set, unordered_set ~> object - template ::value>::type * = nullptr - > - bool read( T &v, bool throw_on_error = false ) { + template + bool read( T &v, bool throw_on_error = false ) requires + std::is_same_v { if( !test_array() ) { return error_or_false( throw_on_error, "Expected json array" ); } @@ -540,10 +538,9 @@ class JsonIn // object ~> containers with unmatching key_type and value_type // map, unordered_map ~> object - template < typename T, typename std::enable_if < - !std::is_same::value >::type * = nullptr - > - bool read( T &m, bool throw_on_error = true ) { + template < typename T> + bool read( T &m, bool throw_on_error = true ) requires( + !std::is_same_v ) { if( !test_object() ) { return error_or_false( throw_on_error, "Expected json object" ); } @@ -665,8 +662,8 @@ class JsonOut // write data to the output stream as JSON void write_null(); - template ::value, int>::type = 0> - void write( T val ) { + template + void write( T val ) requires std::is_fundamental_v { if( need_separator ) { write_separator(); } @@ -715,9 +712,9 @@ class JsonOut } - template ::value, int>::type = 0> - void write( T val ) { - write( static_cast::type>( val ) ); + template + void write( T val ) requires std::is_enum_v { + write( static_cast>( val ) ); } // strings need escaping and quoting @@ -753,8 +750,8 @@ class JsonOut } // enum ~> string - template ::value>::type * = nullptr> - void write_as_string( const E value ) { + template + void write_as_string( const E value ) requires std::is_enum_v { write( io::enum_to_string( value ) ); } @@ -786,19 +783,17 @@ class JsonOut // containers with front() ~> array // vector, deque, forward_list, list - template < typename T, typename std::enable_if < - !std::is_same::value >::type * = nullptr - > - auto write( const T &container ) -> decltype( container.front(), ( void )0 ) { + template < typename T> + auto write( const T &container ) -> decltype( container.front(), + ( void )0 ) requires( !std::is_same_v ) { write_as_array( container ); } // containers with matching key_type and value_type ~> array // set, unordered_set - template ::value>::type * = nullptr - > - void write( const T &container ) { + template + void write( const T &container ) requires + std::is_same_v { write_as_array( container ); } @@ -810,10 +805,9 @@ class JsonOut // containers with unmatching key_type and value_type ~> object // map, unordered_map ~> object - template < typename T, typename std::enable_if < - !std::is_same::value >::type * = nullptr - > - void write( const T &map ) { + template < typename T> + void write( const T &map ) requires( + !std::is_same_v ) { start_object(); for( const auto &it : map ) { write_as_string( it.first ); @@ -976,8 +970,9 @@ class JsonObject std::string get_string( const std::string &name ) const; std::string get_string( const std::string &name, const std::string &fallback ) const; - template::value>::type> - E get_enum_value( const std::string &name, const E fallback ) const { + template + E get_enum_value( const std::string &name, + const E fallback ) const requires std::is_enum_v { if( !has_member( name ) ) { return fallback; } @@ -985,8 +980,8 @@ class JsonObject jsin->seek( verify_position( name ) ); return jsin->get_enum_value(); } - template::value>::type> - E get_enum_value( const std::string &name ) const { + template + E get_enum_value( const std::string &name ) const requires std::is_enum_v { mark_visited( name ); jsin->seek( verify_position( name ) ); return jsin->get_enum_value(); diff --git a/src/mod_tracker.h b/src/mod_tracker.h index 6685e2a8c976..e145718314b4 100644 --- a/src/mod_tracker.h +++ b/src/mod_tracker.h @@ -30,18 +30,20 @@ struct has_src_member().src.emplace_b std::true_type {}; /** Dummy function, for if those conditions are not satisfied */ -template < typename T, typename std::enable_if_t < !has_src_member::value > * = nullptr > +template < typename T> void assign_src( T &, const std::string & ) +requires( !has_src_member::value ) { } /** If those conditions are satisfied, keep track of where this item has been modified */ -template::value > * = nullptr> +template void assign_src( T &def, const std::string &src ) -{ +requires has_src_member::value { // We need to make sure we're keeping where this entity has been loaded // If the id this was last loaded with is not this one, discard the history and start again - if( !def.src.empty() && def.src.back().first != def.id ) { + if( !def.src.empty() && def.src.back().first != def.id ) + { def.src.clear(); } def.src.emplace_back( def.id, mod_id( src ) ); diff --git a/src/output.h b/src/output.h index 82ed73a223e6..aaa108b63e3a 100644 --- a/src/output.h +++ b/src/output.h @@ -747,19 +747,20 @@ void draw_tabs( const catacurses::window &, const std::vector &tab_ // { tab_mode::second_tab, _( "SECOND_TAB" ) }, // }; // draw_tabs( w, tabs, current_tab ); -template>::value>> +template void draw_tabs( const catacurses::window &w, const TabList &tab_list, const CurrentTab ¤t_tab ) -{ +requires std::is_same_v> { std::vector tab_text; std::transform( tab_list.begin(), tab_list.end(), std::back_inserter( tab_text ), - []( const typename TabList::value_type & pair ) { + []( const typename TabList::value_type & pair ) + { return pair.second; } ); auto current_tab_it = std::find_if( tab_list.begin(), tab_list.end(), - [¤t_tab]( const typename TabList::value_type & pair ) { + [¤t_tab]( const typename TabList::value_type & pair ) + { return pair.first == current_tab; } ); assert( current_tab_it != tab_list.end() ); @@ -768,14 +769,14 @@ void draw_tabs( const catacurses::window &w, const TabList &tab_list, // Similar to the above, but where the order of tabs is specified separately // TabList is expected to be a map type. -template>::value>> +template void draw_tabs( const catacurses::window &w, const TabList &tab_list, const TabKeys &keys, const CurrentTab ¤t_tab ) -{ +requires std::is_same_v> { std::vector ordered_tab_list; - for( const auto &key : keys ) { + for( const auto &key : keys ) + { auto it = tab_list.find( key ); assert( it != tab_list.end() ); ordered_tab_list.push_back( *it ); diff --git a/src/pimpl.h b/src/pimpl.h index 65eeda00a986..ddf024da425c 100644 --- a/src/pimpl.h +++ b/src/pimpl.h @@ -16,7 +16,7 @@ class is_pimpl_helper> : public std::true_type { }; template -class is_pimpl : public is_pimpl_helper::type> +class is_pimpl : public is_pimpl_helper> { }; /** @@ -39,10 +39,10 @@ class pimpl : private std::unique_ptr // The new argument serves the same purpose: this constructor should *not* be available when the // argument is a `pimpl` itself (the other copy constructors should be used instead). explicit pimpl() : std::unique_ptr( new T() ) { } - template < typename P, typename ...Args, - typename = typename std::enable_if < !is_pimpl

::value >::type > - explicit pimpl( P && head, Args && - ... args ) : std::unique_ptr( new T( std::forward

( head ), std::forward( args )... ) ) { } + template < typename P, typename ...Args> + explicit pimpl( P &&head, Args && + ... args ) requires( !is_pimpl

::value ) : std::unique_ptr( new T( std::forward

( head ), + std::forward( args )... ) ) { } explicit pimpl( const pimpl &rhs ) : std::unique_ptr( new T( *rhs ) ) { } explicit pimpl( pimpl &&rhs ) noexcept : std::unique_ptr( new T( std::move( *rhs ) ) ) { } diff --git a/src/point_traits.h b/src/point_traits.h index 15760b660cde..70188b6bc9f0 100644 --- a/src/point_traits.h +++ b/src/point_traits.h @@ -33,7 +33,7 @@ struct point_traits { template struct point_traits < Point, - std::enable_if_t < std::is_same::value || std::is_same::value > + std::enable_if_t < std::is_same_v || std::is_same_v > > { static int &x( Point &p ) { return p.x; @@ -58,7 +58,7 @@ struct point_traits < template struct point_traits < Point, - std::enable_if_t < std::is_same::value || std::is_same::value > + std::enable_if_t < std::is_same_v || std::is_same_v > > { static float &x( Point &p ) { return p.x; diff --git a/src/ret_val.h b/src/ret_val.h index 024ddbec7b78..0d7ef99823a0 100644 --- a/src/ret_val.h +++ b/src/ret_val.h @@ -19,11 +19,11 @@ template class ret_val { - static_assert( !std::is_convertible::value, "string values aren't allowed" ); + static_assert( !std::is_convertible_v, "string values aren't allowed" ); template - using is_convertible_to_string = typename - std::enable_if< std::is_convertible::value>::type; + using is_convertible_to_string = + std::enable_if_t< std::is_convertible_v>; public: /** diff --git a/src/rng.h b/src/rng.h index 1075cb485ec1..319b6a94a49a 100644 --- a/src/rng.h +++ b/src/rng.h @@ -135,7 +135,7 @@ class is_std_array_helper> : public std::true_type { }; template -class is_std_array : public is_std_array_helper::type> +class is_std_array : public is_std_array_helper> { }; diff --git a/src/simple_pathfinding.h b/src/simple_pathfinding.h index 09611af96af4..d324e465fd90 100644 --- a/src/simple_pathfinding.h +++ b/src/simple_pathfinding.h @@ -79,9 +79,10 @@ directed_path greedy_path( point source, point dest, point max, * @param max Max permissible coordinates for a point on the path * @param scorer function of (node ¤t, node *previous) that returns node_score. */ -template> +template directed_path greedy_path( const Point &source, const Point &dest, const Point &max, two_node_scoring_fn scorer ) +requires( Point::dimension == 2 ) { directed_path res; const two_node_scoring_fn point_scorer diff --git a/src/stats_tracker.h b/src/stats_tracker.h index 80661673a5bf..fbdfde2c0256 100644 --- a/src/stats_tracker.h +++ b/src/stats_tracker.h @@ -109,7 +109,7 @@ class event_multiset_watcher : public base_watcher template class watcher_set { - static_assert( std::is_base_of::value, + static_assert( std::is_base_of_v, "Watcher must be derived from base_watcher" ); public: void insert( Watcher *watcher ) { diff --git a/src/string_formatter.h b/src/string_formatter.h index 8a1fd15cc20f..3b3e8a6f86eb 100644 --- a/src/string_formatter.h +++ b/src/string_formatter.h @@ -54,44 +54,44 @@ std::string handle_string_format_error(); /**@{*/ // Test for arithmetic type, *excluding* bool. printf can not handle bool, so can't we. template -using is_numeric = typename std::conditional < - std::is_arithmetic::type>::value && - !std::is_same::type, bool>::value, std::true_type, std::false_type >::type; +using is_numeric = std::conditional_t < + std::is_arithmetic_v> && + !std::is_same_v, bool>, std::true_type, std::false_type >; // Test for integer type (not floating point, not bool). template -using is_integer = typename std::conditional < is_numeric::value && - !std::is_floating_point::type>::value, std::true_type, - std::false_type >::type; +using is_integer = std::conditional_t < is_numeric::value && + !std::is_floating_point_v>, std::true_type, + std::false_type >; template -using is_char = typename - std::conditional::type, char>::value, std::true_type, std::false_type>::type; +using is_char = + std::conditional_t, char>, std::true_type, std::false_type>; // Test for std::string type. template -using is_string = typename - std::conditional::type, std::string>::value, std::true_type, std::false_type>::type; +using is_string = + std::conditional_t, std::string>, std::true_type, std::false_type>; // Test for std::string_view type. template -using is_string_view = typename - std::conditional::type, std::string_view>::value, std::true_type, std::false_type>::type; +using is_string_view = + std::conditional_t, std::string_view>, std::true_type, std::false_type>; // Test for c-string type. template -using is_cstring = typename std::conditional < - std::is_same::type, const char *>::value || - std::is_same::type, char *>::value, std::true_type, std::false_type >::type; +using is_cstring = std::conditional_t < + std::is_same_v, const char *> || + std::is_same_v, char *>, std::true_type, std::false_type >; // Test for class translation template -using is_translation = typename std::conditional < - std::is_same::type, translation>::value, std::true_type, - std::false_type >::type; +using is_translation = std::conditional_t < + std::is_same_v, translation>, std::true_type, + std::false_type >; // Test for string_id template class> struct is_instance_of : std::false_type {}; template class TMPL> struct is_instance_of, TMPL> : std::true_type {}; template -using is_string_id = typename std::conditional < - is_instance_of::type, string_id>::value, std::true_type, - std::false_type >::type; +using is_string_id = std::conditional_t < + is_instance_of, string_id>::value, std::true_type, + std::false_type >; template inline typename std::enable_if < is_integer::value &&is_integer::value, @@ -117,7 +117,7 @@ inline typename std::enable_if < std::is_same::value &&std::is_pointer::type>::value, void * >::type convert( RT *, const string_formatter &, T &&value, int ) { - return const_cast::type>::type>::type *> + return const_cast>> *> ( value ); } template @@ -127,9 +127,10 @@ inline typename std::enable_if < std::is_same::value &&is_ return value; } template -inline typename std::enable_if < std::is_same::value -&&is_string_view::value, std::string_view >::type +inline std::string_view convert( RT *, const string_formatter &, T &&value, int ) +requires( std::is_same_v + &&is_string_view::value ) { return value; } @@ -174,12 +175,12 @@ template // NOLINTNEXTLINE(cert-dcl50-cpp) inline RT convert( RT *, const string_formatter &sf, T &&, ... ) { - static_assert( std::is_pointer::type>::value || + static_assert( std::is_pointer_v> || is_numeric::value || is_string::value || is_string_view::value || is_char::value || - std::is_enum::type>::value || + std::is_enum_v> || is_cstring::value || is_translation::value || is_string_id::value, @@ -439,9 +440,9 @@ inline std::string string_format( std::string_view format, Args &&...args ) } } template -inline typename std::enable_if::value, std::string>::type +inline std::string string_format( T &&format, Args &&...args ) -{ +requires cata::is_translation::value { return string_format( format.translated(), std::forward( args )... ); } /**@}*/ diff --git a/src/string_id.h b/src/string_id.h index f13bac82e2d6..ee02dad69128 100644 --- a/src/string_id.h +++ b/src/string_id.h @@ -129,8 +129,8 @@ class string_identity_static #endif {} - template::value>> - explicit string_identity_static( S && id ) + template requires std::is_convertible_v + explicit string_identity_static( S &&id ) : _id( string_id_intern( std::forward( id ) ) ) #ifdef CATA_STRING_ID_DEBUGGING , _string_id( str().c_str() ) @@ -176,8 +176,8 @@ class string_identity_dynamic string_identity_dynamic() = default; - template::value>> - explicit string_identity_dynamic( S && id ) : _id( std::forward( id ) ) {} + template requires std::is_convertible_v + explicit string_identity_dynamic( S &&id ) : _id( std::forward( id ) ) {} inline const std::string &str() const { return _id; @@ -213,8 +213,8 @@ class string_id */ // Beautiful C++11: enable_if makes sure that S is always something that can be used to constructor // a std::string, otherwise a "no matching function to call..." error is generated. - template::value>> - explicit string_id( S && id ) : _id( std::forward( id ) ) {} + template requires std::is_convertible_v + explicit string_id( S &&id ) : _id( std::forward( id ) ) {} /** * Default constructor constructs an empty id string. * Note that this id class does not enforce empty id strings (or any specific string at all) diff --git a/src/string_id_utils.h b/src/string_id_utils.h index e99d7495c5b3..18c52a8aba40 100644 --- a/src/string_id_utils.h +++ b/src/string_id_utils.h @@ -17,13 +17,13 @@ template().begin() )>, typename K = std::decay_t, - typename V = std::decay_t, - std::enable_if_t>::value, int> = 0> + typename V = std::decay_t> std::vector> sorted_lex( Col col ) -{ +requires std::is_same_v> { std::vector> ret; ret.insert( ret.begin(), col.begin(), col.end() ); - std::sort( ret.begin(), ret.end(), []( const auto & a, const auto & b ) { + std::sort( ret.begin(), ret.end(), []( const auto & a, const auto & b ) + { return typename K::LexCmp()( a.first, b.first ); } ); return ret; @@ -35,10 +35,9 @@ std::vector> sorted_lex( Col col ) * @return sorted (using string_id::LexCmp) `std::vector>` */ template().begin() )>, - std::enable_if_t>::value, int> = 0> + typename El = std::decay_t().begin() )>> std::vector sorted_lex( Col col ) -{ +requires std::is_same_v> { std::vector ret; ret.insert( ret.begin(), col.begin(), col.end() ); std::sort( ret.begin(), ret.end(), typename El::LexCmp() ); diff --git a/src/units_utility.h b/src/units_utility.h index 02a16f8d65d2..20defb857f28 100644 --- a/src/units_utility.h +++ b/src/units_utility.h @@ -34,9 +34,9 @@ T divide_round_up( units::quantity num, units::quantity den ) */ units::angle normalize( units::angle, units::angle mod = 360_degrees ); -template::value>* = nullptr> +template units::quantity round_to_multiple_of( units::quantity val, units::quantity of ) -{ +requires std::is_floating_point_v { int multiple = std::lround( val / of ); return multiple * of; } From 7eab273cdcdd5e0d89061438a6d08859691ac2d4 Mon Sep 17 00:00:00 2001 From: scarf Date: Wed, 9 Oct 2024 02:57:39 +0900 Subject: [PATCH 2/2] fix: is_same_v Co-authored-by: OrenAudeles --- src/fmtlib_core.h | 4 ++-- src/fmtlib_format.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/fmtlib_core.h b/src/fmtlib_core.h index 2fb5791c1390..13b1ae076501 100644 --- a/src/fmtlib_core.h +++ b/src/fmtlib_core.h @@ -2185,7 +2185,7 @@ struct wformat_args : basic_format_args { namespace detail { -template < typename Char, FMT_ENABLE_IF( !std::is_same::value ) > +template < typename Char, FMT_ENABLE_IF( !std::is_same_v ) > std::basic_string vformat( basic_string_view format_str, basic_format_args>> args ); @@ -2199,7 +2199,7 @@ void vformat_to( detail::locale_ref loc = {} ); template < typename Char, typename Args, - FMT_ENABLE_IF( !std::is_same::value ) > + FMT_ENABLE_IF( !std::is_same_v ) > inline void vprint_mojibake( std::FILE *, basic_string_view, const Args & ) {} FMT_API void vprint_mojibake( std::FILE *, string_view, format_args ); diff --git a/src/fmtlib_format.h b/src/fmtlib_format.h index 09a08d0b5e1b..221d483d97b2 100644 --- a/src/fmtlib_format.h +++ b/src/fmtlib_format.h @@ -1,5 +1,4 @@ #pragma once -//NOLINT(modernize-type-traits) /* Formatting library for C++