[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
Mon Oct 30 15:11:51 PDT 2023


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

>From 8a0350914ea715b3e669e0f3ee6feccab00bb28c 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] add floating point type check for uniform real distrubtion

---
 .../__random/uniform_real_distribution.h      |  3 +++
 .../traits_mismatch.verify.cpp                | 20 +++++++++++++++++++
 .../test/libcxx/transitive_includes/cxx23.csv |  1 +
 .../test/libcxx/transitive_includes/cxx26.csv |  1 +
 4 files changed, 25 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..37269d767c572ee
--- /dev/null
+++ b/libcxx/test/libcxx/random/random.uniform.real/traits_mismatch.verify.cpp
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 'std::is_floating_point<int>::value'{{.*}}result_type must be floating type}}
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 236dedd186c9268..1035cb418822de8 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -495,6 +495,7 @@ random iosfwd
 random limits
 random numeric
 random string
+random type_traits
 random vector
 random version
 ranges compare
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index 236dedd186c9268..1035cb418822de8 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -495,6 +495,7 @@ random iosfwd
 random limits
 random numeric
 random string
+random type_traits
 random vector
 random version
 ranges compare



More information about the libcxx-commits mailing list