NCBI C++ ToolKit
const_tuple.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef __CT_CONST_TUPLE_HPP_INCLUDED__
2 #define __CT_CONST_TUPLE_HPP_INCLUDED__
3 
4 /* $Id: const_tuple.hpp 93306 2021-03-30 14:56:42Z gotvyans $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Sergiy Gotvyanskyy
30  *
31  * File Description:
32  *
33  */
34 
35 namespace compile_time_bits
36 {
37 template<class... _Types>
38  class const_tuple;
39 
40  template<>
41  class const_tuple<>
42  { // empty tuple
43  public:
44  };
45 
46  template<class _This,
47  class... _Rest>
48  class const_tuple<_This, _Rest...>
49  : private const_tuple<_Rest...>
50  { // recursive tuple definition
51  public:
52  typedef _This _This_type;
53  typedef const_tuple<_Rest...> _Mybase;
54  _This _Myfirst {}; // the stored element
55 
56 
57  constexpr const_tuple() noexcept = default;
58  constexpr const_tuple(const _This& _f, const _Rest&..._rest) noexcept
59  : _Mybase(_rest...), _Myfirst( _f )
60  {
61  }
62  template<typename _T0, typename..._Other>
63  constexpr const_tuple(_T0&& _f0, _Other&&...other) noexcept
64  : _Mybase(std::forward<_Other>(other)...), _Myfirst( std::forward<_T0>(_f0) )
65  {
66  }
67  };
68 } // namespace compile_time_bits
69 
70 namespace std
71 {// these are backported implementations of C++17 methods
72 
73  template<size_t _Index>
74  class tuple_element<_Index, ct_const_tuple<>>
75  { // enforce bounds checking
76  //static_assert(always_false<integral_constant<size_t, _Index>>,
77  // "tuple index out of bounds");
78  };
79 
80  template<class _This, class... _Rest>
81  class tuple_element<0, ct_const_tuple<_This, _Rest...>>
82  { // select first element
83  public:
84  using type = _This;
85  using _Ttype = ct_const_tuple<_This, _Rest...>;
86  };
87 
88  template<size_t _Index, class _This, class... _Rest>
89  class tuple_element<_Index, ct_const_tuple<_This, _Rest...>>
90  : public tuple_element<_Index - 1, ct_const_tuple<_Rest...>>
91  { // recursive tuple_element definition
92  };
93 
94  template<size_t _Index,
95  class... _Types>
96  constexpr const typename tuple_element<_Index, ct_const_tuple<_Types...>>::type&
97  get(const ct_const_tuple<_Types...>& _Tuple) noexcept
98  { // get const reference to _Index element of tuple
99  typedef typename tuple_element<_Index, ct_const_tuple<_Types...>>::_Ttype _Ttype;
100  return (((const _Ttype&)_Tuple)._Myfirst);
101  }
102 }
103 
104 #endif
constexpr const_tuple(_T0 &&_f0, _Other &&...other) noexcept
Definition: const_tuple.hpp:63
#define ct_const_tuple
constexpr const tuple_element< _Index, ct_const_tuple< _Types... > >::type & get(const ct_const_tuple< _Types... > &_Tuple) noexcept
Definition: const_tuple.hpp:97
Definition: type.c:6
#define const
Definition: zconf.h:232
Modified on Fri Sep 20 14:57:53 2024 by modify_doxy.py rev. 669887