Skip to content

Commit 727fef7

Browse files
[libc++] Add floating point type check for uniform real distribution (#70564)
Fixes #62433 Co-authored-by: Louis Dionne <[email protected]>
1 parent 6e8b17d commit 727fef7

15 files changed

+165
-0
lines changed

libcxx/include/__random/cauchy_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2828
template<class _RealType = double>
2929
class _LIBCPP_TEMPLATE_VIS cauchy_distribution
3030
{
31+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32+
"RealType must be a supported floating-point type");
33+
3134
public:
3235
// types
3336
typedef _RealType result_type;

libcxx/include/__random/chi_squared_distribution.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__config>
1313
#include <__random/gamma_distribution.h>
14+
#include <__random/is_valid.h>
1415
#include <iosfwd>
1516
#include <limits>
1617

@@ -26,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2627
template<class _RealType = double>
2728
class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
2829
{
30+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31+
"RealType must be a supported floating-point type");
32+
2933
public:
3034
// types
3135
typedef _RealType result_type;

libcxx/include/__random/exponential_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
template<class _RealType = double>
3030
class _LIBCPP_TEMPLATE_VIS exponential_distribution
3131
{
32+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
33+
"RealType must be a supported floating-point type");
34+
3235
public:
3336
// types
3437
typedef _RealType result_type;

libcxx/include/__random/extreme_value_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2828
template<class _RealType = double>
2929
class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
3030
{
31+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32+
"RealType must be a supported floating-point type");
33+
3134
public:
3235
// types
3336
typedef _RealType result_type;

libcxx/include/__random/fisher_f_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2727
template<class _RealType = double>
2828
class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
2929
{
30+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31+
"RealType must be a supported floating-point type");
32+
3033
public:
3134
// types
3235
typedef _RealType result_type;

libcxx/include/__random/gamma_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
template<class _RealType = double>
3030
class _LIBCPP_TEMPLATE_VIS gamma_distribution
3131
{
32+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
33+
"RealType must be a supported floating-point type");
34+
3235
public:
3336
// types
3437
typedef _RealType result_type;

libcxx/include/__random/is_valid.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323

2424
_LIBCPP_BEGIN_NAMESPACE_STD
2525

26+
// [rand.req.genl]/1.4:
27+
// The effect of instantiating a template that has a template type parameter
28+
// named RealType is undefined unless the corresponding template argument is
29+
// cv-unqualified and is one of float, double, or long double.
30+
31+
template <class>
32+
struct __libcpp_random_is_valid_realtype : false_type {};
33+
template <>
34+
struct __libcpp_random_is_valid_realtype<float> : true_type {};
35+
template <>
36+
struct __libcpp_random_is_valid_realtype<double> : true_type {};
37+
template <>
38+
struct __libcpp_random_is_valid_realtype<long double> : true_type {};
39+
2640
// [rand.req.genl]/1.5:
2741
// The effect of instantiating a template that has a template type parameter
2842
// named IntType is undefined unless the corresponding template argument is

libcxx/include/__random/lognormal_distribution.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
1111

1212
#include <__config>
13+
#include <__random/is_valid.h>
1314
#include <__random/normal_distribution.h>
1415
#include <cmath>
1516
#include <iosfwd>
@@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2728
template<class _RealType = double>
2829
class _LIBCPP_TEMPLATE_VIS lognormal_distribution
2930
{
31+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32+
"RealType must be a supported floating-point type");
33+
3034
public:
3135
// types
3236
typedef _RealType result_type;

libcxx/include/__random/normal_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2828
template<class _RealType = double>
2929
class _LIBCPP_TEMPLATE_VIS normal_distribution
3030
{
31+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32+
"RealType must be a supported floating-point type");
33+
3134
public:
3235
// types
3336
typedef _RealType result_type;

libcxx/include/__random/piecewise_constant_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
template<class _RealType = double>
3030
class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
3131
{
32+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
33+
"RealType must be a supported floating-point type");
34+
3235
public:
3336
// types
3437
typedef _RealType result_type;

libcxx/include/__random/piecewise_linear_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
template<class _RealType = double>
3030
class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
3131
{
32+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
33+
"RealType must be a supported floating-point type");
34+
3235
public:
3336
// types
3437
typedef _RealType result_type;

libcxx/include/__random/student_t_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
template<class _RealType = double>
3030
class _LIBCPP_TEMPLATE_VIS student_t_distribution
3131
{
32+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
33+
"RealType must be a supported floating-point type");
34+
3235
public:
3336
// types
3437
typedef _RealType result_type;

libcxx/include/__random/uniform_real_distribution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2727
template<class _RealType = double>
2828
class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
2929
{
30+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31+
"RealType must be a supported floating-point type");
32+
3033
public:
3134
// types
3235
typedef _RealType result_type;

libcxx/include/__random/weibull_distribution.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__config>
1313
#include <__random/exponential_distribution.h>
14+
#include <__random/is_valid.h>
1415
#include <cmath>
1516
#include <iosfwd>
1617
#include <limits>
@@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2728
template<class _RealType = double>
2829
class _LIBCPP_TEMPLATE_VIS weibull_distribution
2930
{
31+
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32+
"RealType must be a supported floating-point type");
33+
3034
public:
3135
// types
3236
typedef _RealType result_type;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <random>
10+
11+
#include <random>
12+
13+
void test() {
14+
{
15+
std::uniform_real_distribution<int>
16+
baddist; //expected-error@*:* {{RealType must be a supported floating-point type}}
17+
std::uniform_real_distribution<double> okdist;
18+
(void)baddist;
19+
(void)okdist;
20+
}
21+
{
22+
std::exponential_distribution<int>
23+
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
24+
std::exponential_distribution<double> okdist;
25+
(void)baddist;
26+
(void)okdist;
27+
}
28+
29+
{
30+
std::gamma_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
31+
std::gamma_distribution<double> okdist;
32+
(void)baddist;
33+
(void)okdist;
34+
}
35+
36+
{
37+
std::weibull_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
38+
std::weibull_distribution<double> okdist;
39+
(void)baddist;
40+
(void)okdist;
41+
}
42+
43+
{
44+
std::extreme_value_distribution<int>
45+
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
46+
std::extreme_value_distribution<double> okdist;
47+
(void)baddist;
48+
(void)okdist;
49+
}
50+
51+
{
52+
std::normal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
53+
std::normal_distribution<double> okdist;
54+
(void)baddist;
55+
(void)okdist;
56+
}
57+
58+
{
59+
std::lognormal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
60+
std::lognormal_distribution<double> okdist;
61+
(void)baddist;
62+
(void)okdist;
63+
}
64+
65+
{
66+
std::chi_squared_distribution<int>
67+
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
68+
std::chi_squared_distribution<double> okdist;
69+
(void)baddist;
70+
(void)okdist;
71+
}
72+
73+
{
74+
std::cauchy_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
75+
std::cauchy_distribution<double> okdist;
76+
(void)baddist;
77+
(void)okdist;
78+
}
79+
80+
{
81+
std::fisher_f_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
82+
std::fisher_f_distribution<double> okdist;
83+
(void)baddist;
84+
(void)okdist;
85+
}
86+
87+
{
88+
std::student_t_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
89+
std::student_t_distribution<double> okdist;
90+
(void)baddist;
91+
(void)okdist;
92+
}
93+
94+
{
95+
std::piecewise_constant_distribution<int>
96+
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
97+
std::piecewise_constant_distribution<double> okdist;
98+
(void)baddist;
99+
(void)okdist;
100+
}
101+
102+
{
103+
std::piecewise_linear_distribution<int>
104+
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
105+
std::piecewise_linear_distribution<double> okdist;
106+
(void)baddist;
107+
(void)okdist;
108+
}
109+
}

0 commit comments

Comments
 (0)