[libc++] Add floating point type check for uniform real distribution (#70564)

Fixes #62433

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
This commit is contained in:
Nhat Nguyen 2023-12-07 09:06:54 -05:00 committed by GitHub
parent 6e8b17d821
commit 727fef79c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 165 additions and 0 deletions

View File

@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS cauchy_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -11,6 +11,7 @@
#include <__config>
#include <__random/gamma_distribution.h>
#include <__random/is_valid.h>
#include <iosfwd>
#include <limits>
@ -26,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS exponential_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS gamma_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -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

View File

@ -10,6 +10,7 @@
#define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
#include <__config>
#include <__random/is_valid.h>
#include <__random/normal_distribution.h>
#include <cmath>
#include <iosfwd>
@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS lognormal_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS normal_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS student_t_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -11,6 +11,7 @@
#include <__config>
#include <__random/exponential_distribution.h>
#include <__random/is_valid.h>
#include <cmath>
#include <iosfwd>
#include <limits>
@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS weibull_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
"RealType must be a supported floating-point type");
public:
// types
typedef _RealType result_type;

View File

@ -0,0 +1,109 @@
//===----------------------------------------------------------------------===//
//
// 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>
#include <random>
void test() {
{
std::uniform_real_distribution<int>
baddist; //expected-error@*:* {{RealType must be a supported floating-point type}}
std::uniform_real_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::exponential_distribution<int>
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::exponential_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::gamma_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::gamma_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::weibull_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::weibull_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::extreme_value_distribution<int>
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::extreme_value_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::normal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::normal_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::lognormal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::lognormal_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::chi_squared_distribution<int>
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::chi_squared_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::cauchy_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::cauchy_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::fisher_f_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::fisher_f_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::student_t_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::student_t_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::piecewise_constant_distribution<int>
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::piecewise_constant_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
{
std::piecewise_linear_distribution<int>
baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
std::piecewise_linear_distribution<double> okdist;
(void)baddist;
(void)okdist;
}
}