Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Limitations and bugs

#Limitations and bugs II have a nagging doubt about the implicit conversion to/from value_type. There are cases where we could have unchecked arithmetic when we expected it to be checked. For example, given:

Style

#Style ThisThis seems old-fashioned:

#Limitations and bugs I have a nagging doubt about the implicit conversion to/from value_type. There are cases where we could have unchecked arithmetic when we expected it to be checked. For example, given:

#Style This seems old-fashioned:

Limitations and bugs

I have a nagging doubt about the implicit conversion to/from value_type. There are cases where we could have unchecked arithmetic when we expected it to be checked. For example, given:

Style

This seems old-fashioned:

added 27 characters in body
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

All in all, I think the safety would be greatly improved if we declare the conversion to unsafe with the explicit keyword:

All in all, I think the safety would be greatly improved if we declare the conversion explicit:

All in all, I think the safety would be greatly improved if we declare the conversion to unsafe with the explicit keyword:

Undefined concept
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
template<typename T, typename U>
safe_int<std::common_type_t<T,U>> operator+(safe_int<T> a, safe_int<U> b)
{
 using V = std::common_type_t<T,U>;
 return safe_int<V>(std::move(a)) + safe_int<V>(std::move(b));
}
template<typename T, typename U>
 requires Integral<U>std::is_integral_v<U>
safe_int<std::common_type_t<T,U>> operator+(safe_int<T> a, U b)
{
 using V = std::common_type_t<T,U>;
 return safe_int<V>(std::move(a)) + safe_int<V>(b);
}
template<typename T, typename U>
 requires Integral<U>std::is_integral_v<U>
safe_int<std::common_type_t<T,U>> operator+(U a, safe_int<T> b)
{
 return std::b + a;
}
template<typename T, typename U>
safe_int<std::common_type_t<T,U>> operator+(safe_int<T> a, safe_int<U> b)
{
 using V = std::common_type_t<T,U>;
 return safe_int<V>(std::move(a)) + safe_int<V>(std::move(b));
}
template<typename T, typename U>
 requires Integral<U>
safe_int<std::common_type_t<T,U>> operator+(safe_int<T> a, U b)
{
 using V = std::common_type_t<T,U>;
 return safe_int<V>(std::move(a)) + safe_int<V>(b);
}
template<typename T, typename U>
 requires Integral<U>
safe_int<std::common_type_t<T,U>> operator+(U a, safe_int<T> b)
{
 return b + a;
}
template<typename T, typename U>
safe_int<std::common_type_t<T,U>> operator+(safe_int<T> a, safe_int<U> b)
{
 using V = std::common_type_t<T,U>;
 return safe_int<V>(std::move(a)) + safe_int<V>(std::move(b));
}
template<typename T, typename U>
 requires std::is_integral_v<U>
safe_int<std::common_type_t<T,U>> operator+(safe_int<T> a, U b)
{
 using V = std::common_type_t<T,U>;
 return safe_int<V>(std::move(a)) + safe_int<V>(b);
}
template<typename T, typename U>
 requires std::is_integral_v<U>
safe_int<std::common_type_t<T,U>> operator+(U a, safe_int<T> b)
{
 return std::b + a;
}
"requires" is a little easier to read outside the function
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
Loading
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /