ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
integers.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_lang of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace lang {
9
10// #################################################################################################
11// types integer, uinteger, intGap_t and uintGap_t
12// #################################################################################################
13#if DOXYGEN
14
15 #define ALIB_SIZEOF_INTEGER
16
17/// This type specifies platform-independent integral values of the 'natural' bit-size of the
18/// underlying platform. In general, on 32-bit systems this will be 32-bit wide, on 64-bit systems,
19/// 64-bits. Hence, on standard architectures, it has the same bit-size and signedness as
20/// \b std::ptrdiff_t.
21///
22/// The type can be considered as a signed version of \c std::size_t. It is needed because
23/// standard type \c 'int' is not specified in respect to its size. E.g., GNU C++ and Clang compiler
24/// use 32-Bit integers for type \c int, even on 64-Bit platforms.
25///
26/// See also \ref alib::uinteger "alib::uinteger".
27///
28/// \note This documentation is generated using the 64-Bit version of the library. In fact, the
29/// definition as \c int64_t shown here, is not guaranteed platform-specific.
30///
31
32using integer = platform_specific;
33
34/// Unsigned version of \ref alib::integer "alib::integer".
35/// This type should be the same as \c std::size_t on all platforms.
36using uinteger = platform_specific;
37
38
39
40/// This type, together with its counterpart
41/// \ref alib::uintGap_t "alib::uintGap_t" is used to fill a gap that occurs
42/// when method overloads or template specialization are needed for integer types.
43/// The rationale behind and use of this pair of types is best explained with a sample.
44///
45/// Consider the following code:
46///
47/// \snippet "DOX_ENUMS.cpp" DOX_INTXX_DECLARATION
48///
49/// When this is run under 64 Bit - Linux, GNU compiler, the following output is produced:
50/// \verbinclude "DOX_INTXX.txt"
51///
52/// This is not what many C++ programmers would expect: Although type <c>long long</c> is the same
53/// 64-bit type as <c>long</c>, the template method is not seen as specialized by the compiler.
54/// Therefore, we have a "gap" in the definition of specializations for types
55/// <c>long long</c> and <c>unsigned long long</c>.
56///
57/// When compiling and running the same sample code under GNU compiler 32-bit or under
58/// MSVC (Microsoft compiler), 32 or 64-bit, then the gap "moves" to be with types
59/// <c>long</c> and <c>unsigned long</c> instead.
60/// Here, this hurts a lot, because code that uses a simple integer constant \c 1L is not fetched by
61/// the template specializations!
62///
63/// The lesson learned is that two more specializations are needed and that their types are
64/// dependent on the compiler and library used. Because it is not allowed to specialize
65/// simply with all possible extra variants (this would lead to doubly defined methods),
66/// a preprocessor switch that chooses the right types to fill the gap is needed.
67///
68/// This type, together with #uintGap_t, does exactly this: using the preprocessor to select
69/// the right "missing" type.
70///
71/// To fix the sample above, the following two specializations of the template method need to
72/// be added:
73///
74/// \snippet "DOX_ENUMS.cpp" DOX_INTXX_DECLARATION2
75///
76/// When overloading functions with integer types, similar rules apply: To have the complete set
77/// of integer types covered, 10 overloads are needed: from type \b int8_t to type \b int64_t,
78/// type \b %intGap_t and then those five types in two versions, signed and unsigned.
79/// Only with all overloads in place, compiler warnings (on high warning levels),
80/// compiler errors due to ambiguouties and/or the necessity of explicit type conversions are
81/// avoided.
82///
83/// \see
84/// Along with these definitions, preprocessor symbol \ref ALIB_SIZEOF_INTGAP is defined.
85using intGap_t= platform_specific;
86
87 /// Used to complete overwriting methods and template specializations.<br>
88 /// See signed sibling type \ref alib::intGap_t "alib::intGap_t" for more information.
89using uintGap_t= platform_specific;
90
91
92#else // DOXYGEN
93
94
95
96
97// ############# set according to symbols #############
98
99#if ALIB_SIZEOF_INTEGER == 4
100 using integer = int32_t;
101 using uinteger = uint32_t;
102#elif ALIB_SIZEOF_INTEGER == 8
103 using integer = int64_t;
104 using uinteger = uint64_t;
105#else
106 #error "Compiler symbol 'ALIB_SIZEOF_INTEGER' supports only values 4 and 8."
107#endif
108
110using uintGap_t= unsigned ALIB_INTGAP_TYPE;
111
112
113
114// ############# checks #############
115#define ERROR_DETECTING \
116"Cannot detect compilation platform. Please provide Symbols \
117'ALIB_SIZEOF_INTEGER', \
118'ALIB_SIZEOF_INTGAP', \
119'ALIB_INTGAP_TYPE', \
120'ALIB_SIZEOF_LONGDOUBLE_REPORTED' or \
121'ALIB_SIZEOF_LONGDOUBLE_WRITTEN' \
122as documented with ALib User Manual at https://alib.dev"
123
124static_assert( sizeof(integer) == sizeof(uinteger ) , "\nSize mismatch in definition of alib::[u]integer on this platform/compiler.\n" ERROR_DETECTING);
125static_assert( sizeof(integer) == sizeof(size_t ) , "\nSize mismatch in definition of alib::[u]integer on this platform/compiler.\n" ERROR_DETECTING);
126static_assert( sizeof(integer) == sizeof(ptrdiff_t) , "\nSize mismatch in definition of alib::[u]integer on this platform/compiler.\n" ERROR_DETECTING);
127static_assert( sizeof(integer) == sizeof(void* ) , "\nSize mismatch in definition of alib::[u]integer on this platform/compiler.\n" ERROR_DETECTING);
128static_assert( sizeof(integer) == ALIB_SIZEOF_INTEGER , "\nSize mismatch in definition of alib::[u]integer on this platform/compiler.\n" ERROR_DETECTING);
129static_assert( sizeof(intGap_t) == ALIB_SIZEOF_INTGAP , "\nDefinition of symbol ALIB_SIZEOF_INTGAP not adjusted to platform/compiler.\n" ERROR_DETECTING);
130static_assert( sizeof(long double) == ALIB_SIZEOF_LONGDOUBLE_REPORTED, "\nSize mismatch in definition of macro ALIB_SIZEOF_LONGDOUBLE_REPORTED on this platform/compiler.\n" ERROR_DETECTING);
131
132
133
134#if !defined(ALIB_SIZEOF_LONGDOUBLE_WRITTEN)
135 #error "Cannot detect compilation platform. Please provide Symbols \
136'ALIB_SIZEOF_INTEGER', \
137'ALIB_SIZEOF_INTGAP', \
138'ALIB_INTGAP_TYPE', \
139'ALIB_SIZEOF_LONGDOUBLE_REPORTED' or \
140'ALIB_SIZEOF_LONGDOUBLE_WRITTEN' \
141as documented with ALib User Manual at https://alib.dev"
142#endif
143
144#endif // not DOXYGEN
145
146} // namespace alib[::lang]
147
148/// Type alias in namespace \b alib.
150
151/// Type alias in namespace \b alib.
153
154/// Type alias in namespace \b alib.
156
157/// Type alias in namespace \b alib.
159
160} // namespace [alib]
161
162
163// #################################################################################################
164// static assertions for the platform
165// #################################################################################################
166static_assert( sizeof(alib::integer ) == sizeof(alib::uinteger ), "Error in ALib type definitions" );
167static_assert( sizeof(alib::integer ) == sizeof(std::size_t ), "Error in ALib type definitions" );
168static_assert(std::is_signed< alib::integer>::value == std::is_signed<std::ptrdiff_t >::value, "Error in ALib type definitions" );
169static_assert(std::is_signed< alib::integer>::value != std::is_signed<std::size_t >::value, "Error in ALib type definitions" );
170static_assert(std::is_signed<alib::uinteger>::value == std::is_signed<std::size_t >::value, "Error in ALib type definitions" );
171static_assert(std::is_signed<alib::uinteger>::value != std::is_signed<std::ptrdiff_t >::value, "Error in ALib type definitions" );
172
173
174
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_SIZEOF_INTEGER
Definition integers.inl:15
#define ALIB_SIZEOF_INTGAP
Definition prepro.md:24
#define ALIB_INTGAP_TYPE
Definition prepro.md:25
#define ALIB_SIZEOF_LONGDOUBLE_REPORTED
Definition prepro.md:26
platform_specific uintGap_t
Definition integers.inl:89
platform_specific integer
Definition integers.inl:32
platform_specific uinteger
Definition integers.inl:36
platform_specific intGap_t
Definition integers.inl:85
lang::intGap_t intGap_t
Type alias in namespace alib.
Definition integers.inl:155
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
lang::uintGap_t uintGap_t
Type alias in namespace alib.
Definition integers.inl:158
lang::uinteger uinteger
Type alias in namespace alib.
Definition integers.inl:152