|
||||
File indexing completed on 2025-01-18 09:38:17
0001 /*============================================================================= 0002 Copyright (c) 2014 Paul Fultz II 0003 static.h 0004 Distributed under the Boost Software License, Version 1.0. (See accompanying 0005 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0006 ==============================================================================*/ 0007 0008 #ifndef BOOST_HOF_GUARD_FUNCTION_STATIC_H 0009 #define BOOST_HOF_GUARD_FUNCTION_STATIC_H 0010 0011 /// static 0012 /// ====== 0013 /// 0014 /// Description 0015 /// ----------- 0016 /// 0017 /// The `static_` adaptor is a static function adaptor that allows any 0018 /// default-constructible function object to be static-initialized. Functions 0019 /// initialized by `static_` cannot be used in `constexpr` functions. If the 0020 /// function needs to be statically initialized and called in a `constexpr` 0021 /// context, then a `constexpr` constructor needs to be used rather than 0022 /// `static_`. 0023 /// 0024 /// Synopsis 0025 /// -------- 0026 /// 0027 /// template<class F> 0028 /// class static_; 0029 /// 0030 /// Requirements 0031 /// ------------ 0032 /// 0033 /// F must be: 0034 /// 0035 /// * [ConstFunctionObject](ConstFunctionObject) 0036 /// * DefaultConstructible 0037 /// 0038 /// Example 0039 /// ------- 0040 /// 0041 /// #include <boost/hof.hpp> 0042 /// #include <cassert> 0043 /// using namespace boost::hof; 0044 /// 0045 /// // In C++ this class can't be static-initialized, because of the non- 0046 /// // trivial default constructor. 0047 /// struct times_function 0048 /// { 0049 /// double factor; 0050 /// times_function() : factor(2) 0051 /// {} 0052 /// template<class T> 0053 /// T operator()(T x) const 0054 /// { 0055 /// return x*factor; 0056 /// } 0057 /// }; 0058 /// 0059 /// static constexpr static_<times_function> times2 = {}; 0060 /// 0061 /// int main() { 0062 /// assert(6 == times2(3)); 0063 /// } 0064 /// 0065 0066 #include <boost/hof/detail/result_of.hpp> 0067 #include <boost/hof/reveal.hpp> 0068 0069 namespace boost { namespace hof { 0070 0071 template<class F> 0072 struct static_ 0073 { 0074 0075 struct failure 0076 : failure_for<F> 0077 {}; 0078 0079 const F& base_function() const 0080 BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F) 0081 { 0082 static F f; 0083 return f; 0084 } 0085 0086 BOOST_HOF_RETURNS_CLASS(static_); 0087 0088 template<class... Ts> 0089 BOOST_HOF_SFINAE_RESULT(F, id_<Ts>...) 0090 operator()(Ts && ... xs) const 0091 BOOST_HOF_SFINAE_RETURNS(BOOST_HOF_CONST_THIS->base_function()(BOOST_HOF_FORWARD(Ts)(xs)...)); 0092 }; 0093 0094 0095 }} // namespace boost::hof 0096 0097 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |