Namespace: xtd::literals
Library: xtd.core
(with extensions from other modules)
The xtd::literals
namespace provides user-defined literal operators that simplify the creation of commonly used xtd
types.
These literals improve readability and consistency in code, allowing developers to express values in a concise and natural way.
Design PrincipleAll literals are defined under the xtd::literals
namespace, even if the related types belong to other namespaces such as xtd::drawing
or xtd::net
.
This allows a unified using
directive:
using namespace xtd::literals;
Without the need to import module-specific namespaces like xtd::net::literals
or xtd::drawing::literals
.
Even though the operators are grouped under xtd::literals
, their implementations follow a modular layout that respects the logical ownership of types:
xtd.core
├── xtd
│ └── literals
│ ├── byte.hpp
│ ├── char32.hpp
│ ├── version.hpp
│ └── ...
xtd.drawing
├── xtd
│ └── literals
│ └── color.hpp
xtd.net
├── xtd
│ └── literals
│ └── ip.hpp
Each header only includes what it needs and keeps responsibility localized.
Available Literal Operators Literal Target Type Example_b
, _s8
xtd::byte
42_b
or "255"_b
_c
xtd::char32
'A'_c
_s
xtd::ustring
or time_span
"text"_s
or 1.5_s
_h
, _m
, _ms
, _us
, _ns
, _d
xtd::time_span
1_h
, 500_ms
, etc. _vers
xtd::version
"1.2.3"_vers
_color
xtd::drawing::color
"dodger blue"_color
or "FF0000"_color
_country
xtd::forms::country
"Belgium"_country
or "BE"_country
or "BEL"_country
_ip
xtd::net::ip_address
"127.0.0.1"_ip
or "::1"_ip
The following literal operators page shows all xtd literal operators.
Safety and Error HandlingAll string-based literal operators use their respective parse
methods internally:
"1.2.3"_vers → version::parse("1.2.3")
"FF0000"_color → color::parse("FF0000")
Exceptions such as xtd::format_exception
or xtd::argument_exception
will be thrown if parsing fails.
#include <xtd/xtd>
using namespace xtd::literals;
auto main() -> int {
auto version = "1.2.3"_vers;
auto color = "dodger blue"_color;
auto ip = "192.168.1.1"_ip;
auto delay = 2.5_s;
console::write_line("version: {}", version);
console::write_line("color: {}", color);
console::write_line("ip: {}", ip);
console::write_line("delay: {}", delay.total_milliseconds());
}
Notes
xtd::literals
namespace, you can still construct the values manually.#include <xtd/literals/version.hpp>
#include <xtd/literals/color.hpp>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4