[libcxx-commits] [libcxx] [libc++] add floating point type check for uniform real distrubtion (PR #70564)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 21 11:59:33 PST 2023


================
@@ -27,6 +28,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template<class _RealType = double>
 class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
 {
+  static_assert(is_floating_point<_RealType>::value, "result_type must be floating type");
----------------
ldionne wrote:

Add the following to `libcxx/include/__random/is_valid.h`:

```
// [rand.req.genl]/1.4:
// The effect of instantiating a template that has a template type parameter
// named RealType is undefined unless the corresponding template argument is
// cv-unqualified and is one of float, double, or long double.

template<class> struct __libcpp_random_is_valid_realtype : false_type {};
template<> struct __libcpp_random_is_valid_realtype<float> : true_type {};
template<> struct __libcpp_random_is_valid_realtype<double> : true_type {};
template<> struct __libcpp_random_is_valid_realtype<long double> : true_type {};
```

Then, in the distributions mentioned above, add the following `static_assert`:

```
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value, "RealType must be a supported floating-point type");
```


https://github.com/llvm/llvm-project/pull/70564


More information about the libcxx-commits mailing list