Skip to content

[libc++] add floating point type check for uniform real distribution #70564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libcxx/include/__random/cauchy_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions libcxx/include/__random/chi_squared_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <__config>
#include <__random/gamma_distribution.h>
#include <__random/is_valid.h>
#include <iosfwd>
#include <limits>

Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/exponential_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/extreme_value_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/fisher_f_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/gamma_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions libcxx/include/__random/is_valid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions libcxx/include/__random/lognormal_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/normal_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/piecewise_constant_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/piecewise_linear_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/student_t_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libcxx/include/__random/uniform_real_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions libcxx/include/__random/weibull_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <__config>
#include <__random/exponential_distribution.h>
#include <__random/is_valid.h>
#include <cmath>
#include <iosfwd>
#include <limits>
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}