|
|
|||
File indexing completed on 2025-12-15 09:53:05
0001 /*! 0002 @file 0003 Forward declares `boost::hana::string`. 0004 0005 Copyright Louis Dionne 2013-2022 0006 Distributed under the Boost Software License, Version 1.0. 0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) 0008 */ 0009 0010 #ifndef BOOST_HANA_FWD_STRING_HPP 0011 #define BOOST_HANA_FWD_STRING_HPP 0012 0013 #include <boost/hana/config.hpp> 0014 #include <boost/hana/fwd/core/make.hpp> 0015 #include <boost/hana/fwd/core/to.hpp> 0016 0017 0018 namespace boost { namespace hana { 0019 #ifdef BOOST_HANA_DOXYGEN_INVOKED 0020 //! @ingroup group-datatypes 0021 //! Compile-time string. 0022 //! 0023 //! Conceptually, a `hana::string` is like a tuple holding 0024 //! `integral_constant`s of underlying type `char`. However, the 0025 //! interface of `hana::string` is not as rich as that of a tuple, 0026 //! because a string can only hold compile-time characters as opposed 0027 //! to any kind of object. 0028 //! 0029 //! Compile-time strings are used for simple purposes like being keys in a 0030 //! `hana::map` or tagging the members of a `Struct`. However, you might 0031 //! find that `hana::string` does not provide enough functionality to be 0032 //! used as a full-blown compile-time string implementation (e.g. regexp 0033 //! matching or substring finding). Indeed, providing a comprehensive 0034 //! string interface is a lot of job, and it is out of the scope of the 0035 //! library for the time being. 0036 //! 0037 //! 0038 //! @note 0039 //! The representation of `hana::string` is implementation-defined. 0040 //! In particular, one should not take for granted that the template 0041 //! parameters are `char`s. The proper way to access the contents of 0042 //! a `hana::string` as character constants is to use `hana::unpack`, 0043 //! `.c_str()` or `hana::to<char const*>`, as documented below. More 0044 //! details [in the tutorial](@ref tutorial-containers-types). 0045 //! 0046 //! 0047 //! Modeled concepts 0048 //! ---------------- 0049 //! For most purposes, a `hana::string` is functionally equivalent to a 0050 //! tuple holding `Constant`s of underlying type `char`. 0051 //! 0052 //! 1. `Comparable`\n 0053 //! Two strings are equal if and only if they have the same number of 0054 //! characters and characters at corresponding indices are equal. 0055 //! @include example/string/comparable.cpp 0056 //! 0057 //! 2. `Orderable`\n 0058 //! The total order implemented for `Orderable` is the usual 0059 //! lexicographical comparison of strings. 0060 //! @include example/string/orderable.cpp 0061 //! 0062 //! 3. `Monoid`\n 0063 //! Strings form a monoid under concatenation, with the neutral element 0064 //! being the empty string. 0065 //! @include example/string/monoid.cpp 0066 //! 0067 //! 4. `Foldable`\n 0068 //! Folding a string is equivalent to folding the sequence of its 0069 //! characters. 0070 //! @include example/string/foldable.cpp 0071 //! 0072 //! 5. `Iterable`\n 0073 //! Iterating over a string is equivalent to iterating over the sequence 0074 //! of its characters. Also note that `operator[]` can be used instead of 0075 //! the `at` function. 0076 //! @include example/string/iterable.cpp 0077 //! 0078 //! 6. `Searchable`\n 0079 //! Searching through a string is equivalent to searching through the 0080 //! sequence of its characters. 0081 //! @include example/string/searchable.cpp 0082 //! 0083 //! 7. `Hashable`\n 0084 //! The hash of a compile-time string is a type uniquely representing 0085 //! that string. 0086 //! @include example/string/hashable.cpp 0087 //! 0088 //! 0089 //! Conversion to `char const*` 0090 //! --------------------------- 0091 //! A `hana::string` can be converted to a `constexpr` null-delimited 0092 //! string of type `char const*` by using the `c_str()` method or 0093 //! `hana::to<char const*>`. This makes it easy to turn a compile-time 0094 //! string into a runtime string. However, note that this conversion is 0095 //! not an embedding, because `char const*` does not model the same 0096 //! concepts as `hana::string` does. 0097 //! @include example/string/to.cpp 0098 //! 0099 //! Conversion from any Constant holding a `char const*` 0100 //! ---------------------------------------------------- 0101 //! A `hana::string` can be created from any `Constant` whose underlying 0102 //! value is convertible to a `char const*` by using `hana::to`. The 0103 //! contents of the `char const*` are used to build the content of the 0104 //! `hana::string`. 0105 //! @include example/string/from_c_str.cpp 0106 //! 0107 //! Rationale for `hana::string` not being a `Constant` itself 0108 //! ---------------------------------------------------------- 0109 //! The underlying type held by a `hana::string` could be either `char const*` 0110 //! or some other constexpr-enabled string-like container. In the first case, 0111 //! `hana::string` can not be a `Constant` because the models of several 0112 //! concepts would not be respected by the underlying type, causing `value` 0113 //! not to be structure-preserving. Providing an underlying value of 0114 //! constexpr-enabled string-like container type like `std::string_view` 0115 //! would be great, but that's a bit complicated for the time being. 0116 template <typename implementation_defined> 0117 struct string { 0118 // Default-construct a `hana::string`; no-op since `hana::string` is stateless. 0119 constexpr string() = default; 0120 0121 // Copy-construct a `hana::string`; no-op since `hana::string` is stateless. 0122 constexpr string(string const&) = default; 0123 0124 //! Equivalent to `hana::equal` 0125 template <typename X, typename Y> 0126 friend constexpr auto operator==(X&& x, Y&& y); 0127 0128 //! Equivalent to `hana::not_equal` 0129 template <typename X, typename Y> 0130 friend constexpr auto operator!=(X&& x, Y&& y); 0131 0132 //! Equivalent to `hana::less` 0133 template <typename X, typename Y> 0134 friend constexpr auto operator<(X&& x, Y&& y); 0135 0136 //! Equivalent to `hana::greater` 0137 template <typename X, typename Y> 0138 friend constexpr auto operator>(X&& x, Y&& y); 0139 0140 //! Equivalent to `hana::less_equal` 0141 template <typename X, typename Y> 0142 friend constexpr auto operator<=(X&& x, Y&& y); 0143 0144 //! Equivalent to `hana::greater_equal` 0145 template <typename X, typename Y> 0146 friend constexpr auto operator>=(X&& x, Y&& y); 0147 0148 //! Performs concatenation; equivalent to `hana::plus` 0149 template <typename X, typename Y> 0150 friend constexpr auto operator+(X&& x, Y&& y); 0151 0152 //! Equivalent to `hana::at` 0153 template <typename N> 0154 constexpr decltype(auto) operator[](N&& n); 0155 0156 //! Returns a null-delimited C-style string. 0157 static constexpr char const* c_str(); 0158 }; 0159 #else 0160 template <char ...s> 0161 struct string; 0162 #endif 0163 0164 //! Tag representing a compile-time string. 0165 //! @relates hana::string 0166 struct string_tag { }; 0167 0168 #ifdef BOOST_HANA_DOXYGEN_INVOKED 0169 //! Create a compile-time `hana::string` from a parameter pack of `char` 0170 //! `integral_constant`s. 0171 //! @relates hana::string 0172 //! 0173 //! Given zero or more `integral_constant`s of underlying type `char`, 0174 //! `make<string_tag>` creates a `hana::string` containing those characters. 0175 //! This is provided mostly for consistency with the rest of the library, 0176 //! as `hana::string_c` is more convenient to use in most cases. 0177 //! 0178 //! 0179 //! Example 0180 //! ------- 0181 //! @include example/string/make.cpp 0182 template <> 0183 constexpr auto make<string_tag> = [](auto&& ...chars) { 0184 return string<implementation_defined>{}; 0185 }; 0186 #endif 0187 0188 //! Alias to `make<string_tag>`; provided for convenience. 0189 //! @relates hana::string 0190 BOOST_HANA_INLINE_VARIABLE constexpr auto make_string = make<string_tag>; 0191 0192 //! Equivalent to `to<string_tag>`; provided for convenience. 0193 //! @relates hana::string 0194 BOOST_HANA_INLINE_VARIABLE constexpr auto to_string = to<string_tag>; 0195 0196 //! Create a compile-time string from a parameter pack of characters. 0197 //! @relates hana::string 0198 //! 0199 //! 0200 //! Example 0201 //! ------- 0202 //! @include example/string/string_c.cpp 0203 #ifdef BOOST_HANA_DOXYGEN_INVOKED 0204 template <char ...s> 0205 constexpr string<implementation_defined> string_c{}; 0206 #else 0207 template <char ...s> 0208 BOOST_HANA_INLINE_VARIABLE constexpr string<s...> string_c{}; 0209 #endif 0210 0211 //! Create a compile-time string from a string literal. 0212 //! @relates hana::string 0213 //! 0214 //! This macro is a more convenient alternative to `string_c` for creating 0215 //! compile-time strings. However, since this macro uses a lambda 0216 //! internally, it can't be used in an unevaluated context, or where 0217 //! a constant expression is expected before C++17. 0218 //! 0219 //! 0220 //! Example 0221 //! ------- 0222 //! @include example/string/macro.cpp 0223 #ifdef BOOST_HANA_DOXYGEN_INVOKED 0224 auto BOOST_HANA_STRING(s) = see documentation; 0225 #define BOOST_HANA_STRING(s) see documentation 0226 0227 // Note: 0228 // The trick above seems to exploit a bug in Doxygen, which makes the 0229 // BOOST_HANA_STRING macro appear in the related objects of hana::string 0230 // (as we want it to). 0231 #else 0232 // defined in <boost/hana/string.hpp> 0233 #endif 0234 0235 #ifdef BOOST_HANA_CONFIG_ENABLE_STRING_UDL 0236 namespace literals { 0237 //! Creates a compile-time string from a string literal. 0238 //! @relatesalso boost::hana::string 0239 //! 0240 //! The string literal is parsed at compile-time and the result is 0241 //! returned as a `hana::string`. This feature is an extension that 0242 //! is disabled by default; see below for details. 0243 //! 0244 //! @note 0245 //! Only narrow string literals are supported right now; support for 0246 //! fancier types of string literals like wide or UTF-XX might be 0247 //! added in the future if there is a demand for it. See [this issue] 0248 //! [Hana.issue80] if you need this. 0249 //! 0250 //! @warning 0251 //! This user-defined literal is an extension which requires a special 0252 //! string literal operator that is not part of the standard yet. 0253 //! That operator is supported by both Clang and GCC, and several 0254 //! proposals were made for it to enter C++17. However, since it is 0255 //! not standard, it is disabled by default and defining the 0256 //! `BOOST_HANA_CONFIG_ENABLE_STRING_UDL` config macro is required 0257 //! to get this operator. Hence, if you want to stay safe, just use 0258 //! the `BOOST_HANA_STRING` macro instead. If you want to be fast and 0259 //! furious (I do), define `BOOST_HANA_CONFIG_ENABLE_STRING_UDL`. 0260 //! 0261 //! 0262 //! Example 0263 //! ------- 0264 //! @include example/string/literal.cpp 0265 //! 0266 //! [Hana.issue80]: https://github.com/boostorg/hana/issues/80 0267 template <typename CharT, CharT ...s> 0268 constexpr auto operator"" _s(); 0269 } 0270 #endif 0271 }} // end namespace boost::hana 0272 0273 #endif // !BOOST_HANA_FWD_STRING_HPP
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|