1 // Copyright 2019 The Abseil Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ 16 #define ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ 17 18 #include <type_traits> 19 20 #include "absl/meta/type_traits.h" 21 #include "absl/random/internal/distribution_caller.h" 22 #include "absl/random/internal/traits.h" 23 #include "absl/random/internal/uniform_helper.h" 24 25 namespace absl { 26 ABSL_NAMESPACE_BEGIN 27 namespace random_internal { 28 29 // In the absence of an explicitly provided return-type, the template 30 // "uniform_inferred_return_t<A, B>" is used to derive a suitable type, based on 31 // the data-types of the endpoint-arguments {A lo, B hi}. 32 // 33 // Given endpoints {A lo, B hi}, one of {A, B} will be chosen as the 34 // return-type, if one type can be implicitly converted into the other, in a 35 // lossless way. The template "is_widening_convertible" implements the 36 // compile-time logic for deciding if such a conversion is possible. 37 // 38 // If no such conversion between {A, B} exists, then the overload for 39 // absl::Uniform() will be discarded, and the call will be ill-formed. 40 // Return-type for absl::Uniform() when the return-type is inferred. 41 template <typename A, typename B> 42 using uniform_inferred_return_t = 43 absl::enable_if_t<absl::disjunction<is_widening_convertible<A, B>, 44 is_widening_convertible<B, A>>::value, 45 typename std::conditional< 46 is_widening_convertible<A, B>::value, B, A>::type>; 47 48 } // namespace random_internal 49 ABSL_NAMESPACE_END 50 } // namespace absl 51 52 #endif // ABSL_RANDOM_INTERNAL_DISTRIBUTIONS_H_ 53