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

Nhat Nguyen via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 24 23:51:29 PST 2023


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

>From ccf35dbb6f4a92d81c34655ed31456896050914c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sat, 28 Oct 2023 14:30:43 -0400
Subject: [PATCH 1/3] add floating point type check for uniform real
 distrubtion

---
 .../__random/uniform_real_distribution.h      |  3 +++
 .../traits_mismatch.verify.cpp                | 21 +++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp

diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index 1388cef95f39414..236075aaa185cf7 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -12,6 +12,7 @@
 #include <__config>
 #include <__random/generate_canonical.h>
 #include <__random/is_valid.h>
+#include <__type_traits/is_floating_point.h>
 #include <iosfwd>
 #include <limits>
 
@@ -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");
+
 public:
     // types
     typedef _RealType result_type;
diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
new file mode 100644
index 000000000000000..1f36af6dcd4c27c
--- /dev/null
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<_RealType = double>
+// class uniform_real_distribution;
+
+// result_type must be floating type, int type is unsupported
+
+#include <random>
+
+struct test_random : public std::uniform_real_distribution<int> {};
+
+// expected-error@*:* {{static assertion failed due to requirement
+// 'is_floating_point<int>::value': result_type must be floating type}}

>From 19c47c3f4a91c86f833efbe56fb427a5edf2c5fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sun, 5 Nov 2023 12:50:36 -0500
Subject: [PATCH 2/3] fix format

---
 .../random/random.uniform.real/traits_mismatch.verify.cpp      | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
index 1f36af6dcd4c27c..860200e3868b159 100644
--- a/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -17,5 +17,4 @@
 
 struct test_random : public std::uniform_real_distribution<int> {};
 
-// expected-error@*:* {{static assertion failed due to requirement
-// 'is_floating_point<int>::value': result_type must be floating type}}
+// expected-error@*:* {{static assertion failed due to requirement 'is_floating_point<int>::value': result_type must be floating type}}

>From 78a7b60aec80453c8ca5486b0d157ba97e8b28c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <“nhat7203 at gmail.com”>
Date: Sat, 25 Nov 2023 02:50:51 -0500
Subject: [PATCH 3/3] use static assert from is_valid

---
 libcxx/include/__random/is_valid.h                 | 14 ++++++++++++++
 .../include/__random/uniform_real_distribution.h   |  4 ++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__random/is_valid.h b/libcxx/include/__random/is_valid.h
index 113452bc5d35aa2..bde4733099d6884 100644
--- a/libcxx/include/__random/is_valid.h
+++ b/libcxx/include/__random/is_valid.h
@@ -23,6 +23,20 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+// [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 {};
+
 // [rand.req.genl]/1.5:
 // The effect of instantiating a template that has a template type parameter
 // named IntType is undefined unless the corresponding template argument is
diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index 236075aaa185cf7..48baa951388be76 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -12,7 +12,6 @@
 #include <__config>
 #include <__random/generate_canonical.h>
 #include <__random/is_valid.h>
-#include <__type_traits/is_floating_point.h>
 #include <iosfwd>
 #include <limits>
 
@@ -28,7 +27,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");
+  static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+                "RealType must be a supported floating-point type");
 
 public:
     // types



More information about the libcxx-commits mailing list