Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:26

0001 
0002 #ifndef BOOST_CONTRACT_DETAIL_STATIC_LOCAL_VAR_HPP_
0003 #define BOOST_CONTRACT_DETAIL_STATIC_LOCAL_VAR_HPP_
0004 
0005 // Copyright (C) 2008-2018 Lorenzo Caminiti
0006 // Distributed under the Boost Software License, Version 1.0 (see accompanying
0007 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
0008 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
0009 
0010 namespace boost { namespace contract { namespace detail {
0011 
0012 // This is used to hold the state of this library (already checking assertions,
0013 // failure handers, mutexes, etc.). Local static variables are used instead of
0014 // global or class-level static variables to avoid ODR errors when this library
0015 // is used as header-only.
0016 
0017 // Use T's default constructor to init the local var.
0018 template<typename Tag, typename T>
0019 struct static_local_var {
0020     static T& ref() {
0021         static T data;
0022         return data;
0023     }
0024 };
0025 
0026 // Use `init` param to init local var (Init same as or convertible to T).
0027 // NOTE: Template specializations could be used to program both this and the
0028 // template above together but some pre-C++11 compilers give errors (e.g., Clang
0029 // without -std=c++11), plus the `_init` postfix is more readable at call site.
0030 template<typename Tag, typename T, typename Init, Init init>
0031 struct static_local_var_init {
0032     static T& ref() {
0033         static T data = init;
0034         return data;
0035     }
0036 };
0037 
0038 } } } // namespace
0039        
0040 #endif // #include guard
0041