From patchwork Fri Jul 16 05:53:07 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [v3,constexpr] intial conversion to constexpr Date: Thu, 15 Jul 2010 19:53:07 -0000 From: Benjamin Kosnik X-Patchwork-Id: 59068 Message-Id: <20100715225307.18cd3099@shotwell> To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Here is a preliminary patch for libstdc++/* files on the constexpr branch. As is, this doesn't compile: ICE's in atomics, some issues in random with the max() conversion, etc. But is is a useful starting point. Testing the constexpr branch on linux, I see some extra fails. I'm assuming these are expected. Should I work on adding in the additional tests I did some months ago to the g++ testsuite? I suppose the branch should be documented as well. -benjamin 2010-07-15 Benjamin Kosnik * include/std/future: Preliminary constexpr additions. * include/std/atomic: Same. * include/std/chrono: Same. * include/bits/atomic_0.h: Same. * include/bits/atomic_2.h: Same. * include/bits/random.h: Same. Index: include/std/future =================================================================== --- include/std/future (revision 162241) +++ include/std/future (working copy) @@ -64,18 +64,22 @@ no_state }; + /// Specialization. template<> struct is_error_code_enum : public true_type { }; /// Points to a statically-allocated object derived from error_category. - extern const error_category* const future_category; + const error_category& + future_category(); - // TODO: requires constexpr - inline error_code make_error_code(future_errc __errc) + /// Overload for make_error_code. + inline error_code + make_error_code(future_errc __errc) { return error_code(static_cast(__errc), *future_category); } - // TODO: requires constexpr - inline error_condition make_error_condition(future_errc __errc) + /// Overload for make_error_condition. + inline error_condition + make_error_condition(future_errc __errc) { return error_condition(static_cast(__errc), *future_category); } /** @@ -116,8 +120,22 @@ template class promise; - enum class launch { any, async, sync }; + /// Launch code for futures + enum class launch + { + any, + async, + sync + }; + /// Status code for futures + enum class future_status + { + ready, + timeout, + deferred + }; + template future::type> async(launch __policy, _Fn&& __fn, _Args&&... __args); @@ -1264,6 +1282,7 @@ } }; + /// swap template inline void swap(packaged_task<_Res(_ArgTypes...)>& __x, @@ -1324,6 +1343,7 @@ thread _M_thread; }; + /// async template future::type> async(launch __policy, _Fn&& __fn, _Args&&... __args) @@ -1345,6 +1365,7 @@ return future(__state); } + /// async, potential overload template inline typename enable_if::type, launch>::value, Index: include/std/atomic =================================================================== --- include/std/atomic (revision 162241) +++ include/std/atomic (working copy) @@ -38,8 +38,8 @@ # include #endif -#include #include +#include _GLIBCXX_BEGIN_NAMESPACE(std) @@ -94,7 +94,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(_Tp __i) : _M_i(__i) { } + constexpr atomic(_Tp __i) : _M_i(__i) { } operator _Tp() const; @@ -138,7 +138,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(_Tp* __v) : atomic_address(__v) { } + constexpr atomic(_Tp* __v) : atomic_address(__v) { } void store(_Tp*, memory_order = memory_order_seq_cst); @@ -211,7 +211,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -229,7 +229,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -247,7 +247,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -265,7 +265,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -283,7 +283,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -301,7 +301,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -319,7 +319,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -337,7 +337,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -355,7 +355,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -373,7 +373,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -391,7 +391,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -409,7 +409,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -427,7 +427,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -445,7 +445,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -463,7 +463,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; @@ -481,7 +481,7 @@ atomic(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; - atomic(__integral_type __i) : __base_type(__i) { } + constexpr atomic(__integral_type __i) : __base_type(__i) { } using __base_type::operator __integral_type; using __base_type::operator=; Index: include/std/chrono =================================================================== --- include/std/chrono (revision 162241) +++ include/std/chrono (working copy) @@ -173,15 +173,15 @@ template struct duration_values { - static const _Rep + static constexpr _Rep zero() { return _Rep(0); } - static const _Rep + static constexpr _Rep max() { return numeric_limits<_Rep>::max(); } - static const _Rep + static constexpr _Rep min() { return numeric_limits<_Rep>::min(); } }; @@ -209,20 +209,20 @@ typedef _Period period; // 20.8.3.1 construction / copy / destroy - duration() = default; + constexpr duration() = default; template::value && (treat_as_floating_point::value || !treat_as_floating_point<_Rep2>::value)>::type> - explicit duration(const _Rep2& __rep) + constexpr explicit duration(const _Rep2& __rep) : __r(static_cast(__rep)) { } template::value || (ratio_divide<_Period2, period>::type::den == 1 && !treat_as_floating_point<_Rep2>::value)>::type> - duration(const duration<_Rep2, _Period2>& __d) + constexpr duration(const duration<_Rep2, _Period2>& __d) : __r(duration_cast(__d).count()) { } ~duration() = default; @@ -230,16 +230,16 @@ duration& operator=(const duration&) = default; // 20.8.3.2 observer - rep + constexpr rep count() const { return __r; } // 20.8.3.3 arithmetic - duration + constexpr duration operator+() const { return *this; } - duration + constexpr duration operator-() const { return duration(-__r); } @@ -313,16 +313,15 @@ } // 20.8.3.4 special values - // TODO: These should be constexprs. - static const duration + static constexpr duration zero() { return duration(duration_values::zero()); } - static const duration + static constexpr duration min() { return duration(duration_values::min()); } - static const duration + static constexpr duration max() { return duration(duration_values::max()); } @@ -530,12 +529,11 @@ } // special values - // TODO: These should be constexprs. - static const time_point + static constexpr time_point min() { return time_point(duration::min()); } - static const time_point + static constexpr time_point max() { return time_point(duration::max()); } @@ -660,13 +658,10 @@ chrono::seconds(__t))); } - // TODO: requires constexpr - /* static_assert( system_clock::duration::min() < system_clock::duration::zero(), "a clock's minimum duration cannot be less than its epoch"); - */ }; #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC Index: include/bits/atomic_0.h =================================================================== --- include/bits/atomic_0.h (revision 162241) +++ include/bits/atomic_0.h (working copy) @@ -1,6 +1,6 @@ // -*- C++ -*- header. -// Copyright (C) 2008, 2009 +// Copyright (C) 2008, 2009, 2010 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free @@ -111,7 +111,7 @@ atomic_address(const atomic_address&) = delete; atomic_address& operator=(const atomic_address&) volatile = delete; - atomic_address(void* __v) { _M_i = __v; } + constexpr atomic_address(void* __v) { _M_i = __v; } bool is_lock_free() const @@ -255,7 +255,7 @@ __atomic_base& operator=(const __atomic_base&) volatile = delete; // Requires __integral_type convertible to _M_base._M_i. - __atomic_base(__integral_type __i) { _M_i = __i; } + constexpr __atomic_base(__integral_type __i) { _M_i = __i; } operator __integral_type() const { return load(); } @@ -399,7 +399,7 @@ atomic_bool(const atomic_bool&) = delete; atomic_bool& operator=(const atomic_bool&) volatile = delete; - atomic_bool(bool __i) : _M_base(__i) { } + constexpr atomic_bool(bool __i) : _M_base(__i) { } bool operator=(bool __i) Index: include/bits/atomic_2.h =================================================================== --- include/bits/atomic_2.h (revision 162241) +++ include/bits/atomic_2.h (working copy) @@ -89,7 +89,7 @@ atomic_address(const atomic_address&) = delete; atomic_address& operator=(const atomic_address&) volatile = delete; - atomic_address(void* __v) { _M_i = __v; } + constexpr atomic_address(void* __v) { _M_i = __v; } bool is_lock_free() const @@ -233,7 +233,7 @@ __atomic_base& operator=(const __atomic_base&) volatile = delete; // Requires __integral_type convertible to _M_base._M_i. - __atomic_base(__integral_type __i) { _M_i = __i; } + constexpr __atomic_base(__integral_type __i) { _M_i = __i; } operator __integral_type() const { return load(); } @@ -399,7 +399,7 @@ atomic_bool(const atomic_bool&) = delete; atomic_bool& operator=(const atomic_bool&) volatile = delete; - atomic_bool(bool __i) : _M_base(__i) { } + constexpr atomic_bool(bool __i) : _M_base(__i) { } bool operator=(bool __i) Index: include/bits/random.h =================================================================== --- include/bits/random.h (revision 162241) +++ include/bits/random.h (working copy) @@ -166,12 +166,12 @@ typedef _UIntType result_type; /** The multiplier. */ - static const result_type multiplier = __a; + static constexpr result_type multiplier = __a; /** An increment. */ - static const result_type increment = __c; + static constexpr result_type increment = __c; /** The modulus. */ - static const result_type modulus = __m; - static const result_type default_seed = 1u; + static constexpr result_type modulus = __m; + static constexpr result_type default_seed = 1u; /** * @brief Constructs a %linear_congruential_engine random number @@ -225,8 +225,8 @@ * * @todo This should be constexpr. */ - result_type - min() const + static constexpr result_type + min() { return __c == 0u ? 1u : 0u; } /** @@ -234,8 +234,8 @@ * * @todo This should be constexpr. */ - result_type - max() const + static constexpr result_type + max() { return __m - 1u; } /** @@ -399,20 +399,20 @@ typedef _UIntType result_type; // parameter values - static const size_t word_size = __w; - static const size_t state_size = __n; - static const size_t shift_size = __m; - static const size_t mask_bits = __r; - static const result_type xor_mask = __a; - static const size_t tempering_u = __u; - static const result_type tempering_d = __d; - static const size_t tempering_s = __s; - static const result_type tempering_b = __b; - static const size_t tempering_t = __t; - static const result_type tempering_c = __c; - static const size_t tempering_l = __l; - static const result_type initialization_multiplier = __f; - static const result_type default_seed = 5489u; + static constexpr size_t word_size = __w; + static constexpr size_t state_size = __n; + static constexpr size_t shift_size = __m; + static constexpr size_t mask_bits = __r; + static constexpr result_type xor_mask = __a; + static constexpr size_t tempering_u = __u; + static constexpr result_type tempering_d = __d; + static constexpr size_t tempering_s = __s; + static constexpr result_type tempering_b = __b; + static constexpr size_t tempering_t = __t; + static constexpr result_type tempering_c = __c; + static constexpr size_t tempering_l = __l; + static constexpr result_type initialization_multiplier = __f; + static constexpr result_type default_seed = 5489u; // constructors and member function explicit @@ -444,8 +444,8 @@ * * @todo This should be constexpr. */ - result_type - min() const + static constexpr result_type + min() { return 0; }; /** @@ -453,8 +453,8 @@ * * @todo This should be constexpr. */ - result_type - max() const + static constexpr result_type + max() { return __detail::_Shift<_UIntType, __w>::__value - 1; } /** @@ -605,10 +605,10 @@ typedef _UIntType result_type; // parameter values - static const size_t word_size = __w; - static const size_t short_lag = __s; - static const size_t long_lag = __r; - static const result_type default_seed = 19780503u; + static constexpr size_t word_size = __w; + static constexpr size_t short_lag = __s; + static constexpr size_t long_lag = __r; + static constexpr result_type default_seed = 19780503u; /** * @brief Constructs an explicitly seeded % subtract_with_carry_engine @@ -660,8 +660,8 @@ * * @todo This should be constexpr. */ - result_type - min() const + static constexpr result_type + min() { return 0; } /** @@ -670,8 +670,8 @@ * * @todo This should be constexpr. */ - result_type - max() const + static constexpr result_type + max() { return __detail::_Shift<_UIntType, __w>::__value - 1; } /** @@ -791,8 +791,8 @@ typedef typename _RandomNumberEngine::result_type result_type; // parameter values - static const size_t block_size = __p; - static const size_t used_block = __r; + static constexpr size_t block_size = __p; + static constexpr size_t used_block = __r; /** * @brief Constructs a default %discard_block_engine engine. @@ -894,8 +894,8 @@ * * @todo This should be constexpr. */ - result_type - min() const + static constexpr result_type + min() { return _M_b.min(); } /** @@ -903,8 +903,8 @@ * * @todo This should be constexpr. */ - result_type - max() const + static constexpr result_type + max() { return _M_b.max(); } /** @@ -1109,8 +1109,8 @@ * * @todo This should be constexpr. */ - result_type - min() const + static constexpr result_type + min() { return 0U; } /** @@ -1118,8 +1118,8 @@ * * @todo This should be constexpr. */ - result_type - max() const + static constexpr result_type + max() { return __detail::_Shift<_UIntType, __w>::__value - 1; } /** @@ -1240,7 +1240,7 @@ /** The type of the generated random value. */ typedef typename _RandomNumberEngine::result_type result_type; - static const size_t table_size = __k; + static constexpr size_t table_size = __k; /** * @brief Constructs a default %shuffle_order_engine engine. @@ -1345,8 +1345,8 @@ * * @todo This should be constexpr. */ - result_type - min() const + static constexpr result_type + min() { return _M_b.min(); } /** @@ -1354,8 +1354,8 @@ * * @todo This should be constexpr. */ - result_type - max() const + static constexpr result_type + max() { return _M_b.max(); } /** @@ -1567,12 +1567,12 @@ #endif - result_type - min() const + static constexpr result_type + min() { return std::numeric_limits::min(); } - result_type - max() const + static constexpr result_type + max() { return std::numeric_limits::max(); } double @@ -1614,7 +1614,7 @@ */ /** - * @addtogroup random_distributions_uniform Uniform + * @addtogroup random_distributions_uniform Uniform Distributions * @ingroup random_distributions * @{ */ @@ -1972,7 +1972,7 @@ /* @} */ // group random_distributions_uniform /** - * @addtogroup random_distributions_normal Normal + * @addtogroup random_distributions_normal Normal Distributions * @ingroup random_distributions * @{ */ @@ -3226,7 +3226,7 @@ /* @} */ // group random_distributions_normal /** - * @addtogroup random_distributions_bernoulli Bernoulli + * @addtogroup random_distributions_bernoulli Bernoulli Distributions * @ingroup random_distributions * @{ */ @@ -3957,7 +3957,7 @@ /* @} */ // group random_distributions_bernoulli /** - * @addtogroup random_distributions_poisson Poisson + * @addtogroup random_distributions_poisson Poisson Distributions * @ingroup random_distributions * @{ */