ipaddress 1.1.0
Loading...
Searching...
No Matches
ip_address Class Reference

A class that represents an IP address, supporting both IPv4 and IPv6 formats. More...

#include <ip-any-address.hpp>

Public Types

using base_type_ipv4 = typename ipv4_address::base_type
 Base type for IPv4 address storage.
 
using base_type_ipv6 = typename ipv6_address::base_type
 Base type for IPv6 address storage.
 
using uint_type_ipv4 = typename ipv4_address::uint_type
 Unsigned integer type for IPv4 address representation.
 
using uint_type_ipv6 = typename ipv6_address::uint_type
 Unsigned integer type for IPv6 address representation.
 

Public Member Functions

constexpr inline ip_version version () const noexcept
 Retrieves the version of the IP address.
 
constexpr inline bool is_multicast () const noexcept
 Checks if the IP address is a multicast address.
 
constexpr inline bool is_private () const noexcept
 Checks if the IP address is a private address.
 
constexpr inline bool is_global () const noexcept
 Checks if the IP address is a global address.
 
constexpr inline bool is_reserved () const noexcept
 Checks if the IP address is a reserved address.
 
constexpr inline bool is_loopback () const noexcept
 Checks if the IP address is a loopback address.
 
constexpr inline bool is_link_local () const noexcept
 Checks if the IP address is link-local.
 
constexpr inline bool is_unspecified () const noexcept
 Checks if the IP address is unspecified.
 
constexpr inline bool is_site_local () const noexcept
 Checks if the IPv6 address is a site-local address.
 
constexpr inline bool is_v4 () const noexcept
 Checks if the IP address is an IPv4 address.
 
constexpr inline bool is_v6 () const noexcept
 Checks if the IP address is an IPv6 address.
 
constexpr inline size_t size () const noexcept
 Retrieves the size of the IP address.
 
constexpr inline size_t hash () const noexcept
 Computes a hash value for the IP address.
 
constexpr inline optional< ipv4_addressipv4_mapped () const noexcept
 Determines if the IPv6 address is an IPv4-mapped address.
 
constexpr inline optional< ipv4_addresssixtofour () const noexcept
 Determines if the IPv6 address is a 6to4 address.
 
constexpr inline optional< std::pair< ipv4_address, ipv4_address > > teredo () const noexcept
 Determines if the IPv6 address is a Teredo address.
 
constexpr inline optional< ipv4_addressv4 () const noexcept
 Retrieves the IPv4 address.
 
constexpr inline optional< ipv6_addressv6 () const noexcept
 Retrieves the IPv6 address.
 
constexpr inline const uint8_t * data () const noexcept
 Retrieves the raw data of the IP address in network byte order (big-endian).
 
constexpr inline ip_address () noexcept
 Default constructor.
 
constexpr inline ip_address (const ipv4_address &ipv4) noexcept
 Constructor from an ipv4_address.
 
constexpr inline ip_address (const ipv6_address &ipv6) noexcept
 Constructor from an ipv6_address.
 
constexpr inline ip_address (const base_type_ipv4 &bytes) noexcept
 Constructor from byte array for IPv4.
 
constexpr inline ip_address (const base_type_ipv6 &bytes) noexcept
 Constructor from byte array for IPv6.
 
constexpr inline uint32_t to_uint32 () const noexcept
 Converts the IP address to a uint32_t.
 
constexpr inline uint128_t to_uint128 () const noexcept
 Converts the IP address to a uint128_t.
 
inline std::string to_string (format fmt=format::compressed) const
 Converts the IP address to a string.
 
inline std::wstring to_wstring (format fmt=format::compressed) const
 Converts the IP address to a string.
 
inline std::u16string to_u16string (format fmt=format::compressed) const
 Converts the IP address to a string.
 
inline std::u32string to_u32string (format fmt=format::compressed) const
 Converts the IP address to a string.
 
inline std::u8string to_u8string (format fmt=format::compressed) const
 Converts the IP address to a string.
 
inline std::string reverse_pointer () const
 Generates a reverse DNS lookup pointer for the IP address.
 
constexpr inline void swap (ip_address &ip) noexcept
 Swaps the contents of this ip_address with another ip_address.
 
constexpr inline void set_scope_id (std::string_view scope_id)
 Sets the scope identifier of the IPv6 address using a string view.
 
constexpr inline void set_scope_id (std::wstring_view scope_id)
 Sets the scope identifier of the IPv6 address using a wide string view.
 
constexpr inline void set_scope_id (std::u8string_view scope_id)
 Sets the scope identifier of the IPv6 address using UTF-8 string view.
 
constexpr inline void set_scope_id (std::u16string_view scope_id)
 Sets the scope identifier of the IPv6 address using UTF-16 string view.
 
constexpr inline void set_scope_id (std::u32string_view scope_id)
 Sets the scope identifier of the IPv6 address using UTF-32 string view.
 
constexpr inline void set_scope_id (std::string_view scope_id, error_code &code) noexcept
 Sets the scope identifier of the IPv6 address using a string view and reports any errors encountered.
 
constexpr inline void set_scope_id (std::wstring_view scope_id, error_code &code) noexcept
 Sets the scope identifier of the IPv6 address using a wide string view and reports any errors encountered.
 
constexpr inline void set_scope_id (std::u8string_view scope_id, error_code &code) noexcept
 Sets the scope identifier of the IPv6 address using a UTF-8 string view and reports any errors encountered.
 
constexpr inline void set_scope_id (std::u16string_view scope_id, error_code &code) noexcept
 Sets the scope identifier of the IPv6 address using a UTF-16 string view and reports any errors encountered.
 
constexpr inline void set_scope_id (std::u32string_view scope_id, error_code &code) noexcept
 Sets the scope identifier of the IPv6 address using a UTF-32 string view and reports any errors encountered.
 
template<typename T , size_t N>
constexpr inline void set_scope_id (const T(&scope_id)[N]) noexcept(noexcept(ipv6_address().set_scope_id(scope_id)))
 Sets the scope identifier of the IPv6 address.
 
constexpr inline scope get_scope_id () const noexcept
 Retrieves the scope identifier of the IPv6 address.
 
constexpr inline operator uint32_t () const noexcept
 Converts the IP address to a uint32_t.
 
constexpr inline operator uint128_t () const noexcept
 Converts the IP address to a uint128_t.
 
template<typename T >
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > > () const
 Converts the IP address to a string.
 
constexpr inline bool operator== (const ip_address &rhs) const noexcept
 Equality comparison operator.
 
constexpr inline bool operator!= (const ip_address &rhs) const noexcept
 Inequality comparison operator.
 
constexpr inline bool operator< (const ip_address &rhs) const noexcept
 Less than comparison operator.
 
constexpr inline bool operator> (const ip_address &rhs) const noexcept
 Greater than comparison operator.
 
constexpr inline bool operator<= (const ip_address &rhs) const noexcept
 Less than or equal to comparison operator.
 
constexpr inline bool operator>= (const ip_address &rhs) const noexcept
 Greater than or equal to comparison operator.
 

Static Public Member Functions

static constexpr inline ip_address from_bytes (const base_type_ipv4 &bytes) noexcept
 Static factory method to create an ip_address from byte array for IPv4.
 
static constexpr inline ip_address from_bytes (const base_type_ipv6 &bytes) noexcept
 Static factory method to create an ip_address from byte array for IPv6.
 
static constexpr inline ip_address from_bytes (const uint8_t *bytes, size_t byte_count, ip_version version) noexcept
 Static factory method to create an ip_address from raw bytes.
 
static constexpr inline ip_address from_uint (uint_type_ipv4 ip) noexcept
 Static factory method to create an ip_address from a uint_type_ipv4.
 
static constexpr inline ip_address from_uint (const uint_type_ipv6 &ip) noexcept
 Static factory method to create an ip_address from a uint_type_ipv6.
 
template<fixed_string FixedString>
static consteval inline ip_address parse () noexcept
 Static method template to parse an IP address from a fixed string at compile time.
 
static constexpr inline ip_address parse (std::string_view address)
 Parses an IP address from a string view.
 
static constexpr inline ip_address parse (std::wstring_view address)
 Parses an IP address from a wide string view.
 
static constexpr inline ip_address parse (std::u8string_view address)
 Parses an IP address from a UTF-8 string view.
 
static constexpr inline ip_address parse (std::u16string_view address)
 Parses an IP address from a UTF-16 string view.
 
static constexpr inline ip_address parse (std::u32string_view address)
 Parses an IP address from a UTF-32 string view.
 
static constexpr inline ip_address parse (std::string_view address, error_code &code) noexcept
 Parses an IP address from a string view and reports errors through an error code.
 
static constexpr inline ip_address parse (std::wstring_view address, error_code &code) noexcept
 Parses an IP address from a wide string view and reports errors through an error code.
 
static constexpr inline ip_address parse (std::u8string_view address, error_code &code) noexcept
 Parses an IP address from a UTF-8 string view and reports errors through an error code.
 
static constexpr inline ip_address parse (std::u16string_view address, error_code &code) noexcept
 Parses an IP address from a UTF-16 string view and reports errors through an error code.
 
static constexpr inline ip_address parse (std::u32string_view address, error_code &code) noexcept
 Parses an IP address from a UTF-32 string view and reports errors through an error code.
 
template<typename T , size_t N>
static constexpr inline ip_address parse (const T(&address)[N])
 Parses an IP address from a character array.
 
template<typename T , size_t N>
static constexpr inline ip_address parse (const T(&address)[N], error_code &code) noexcept
 Parses an IP address from a character array and reports errors through an error code.
 

Detailed Description

A class that represents an IP address, supporting both IPv4 and IPv6 formats.

The ip_address class is a union-type structure that encapsulates both ipv4_address and ipv6_address, allowing for the representation and manipulation of both IPv4 and IPv6 addresses. It provides a set of functions and type aliases that facilitate the handling of IP addresses in a network context, making it a versatile tool for developers working with IP-based communications.

The class ensures that the size of its instances will always be large enough to store an IPv6 address, which is the larger of the two address types. This design guarantees that an ip_address object can store any valid IP address, regardless of its version, without the need for separate storage mechanisms.

Constructor & Destructor Documentation

◆ ip_address() [1/5]

constexpr inline ip_address ( )
inlinenoexcept

Default constructor.

Constructs an ip_address object with an unspecified ip address with version IPv4.

◆ ip_address() [2/5]

constexpr inline ip_address ( const ipv4_address & ipv4)
inlinenoexcept

Constructor from an ipv4_address.

Constructs an ip_address object with the specified IPv4 address.

Parameters
[in]ipv4An ipv4_address object to initialize the ip_address.

◆ ip_address() [3/5]

constexpr inline ip_address ( const ipv6_address & ipv6)
inlinenoexcept

Constructor from an ipv6_address.

Constructs an ip_address object with the specified IPv6 address.

Parameters
[in]ipv6An ipv6_address object to initialize the ip_address.

◆ ip_address() [4/5]

constexpr inline ip_address ( const base_type_ipv4 & bytes)
inlineexplicitnoexcept

Constructor from byte array for IPv4.

Constructs an ip_address object with the specified byte array for an IPv4 address.

Parameters
[in]bytesA base_type_ipv4 object representing the byte array of the IPv4 address.

◆ ip_address() [5/5]

constexpr inline ip_address ( const base_type_ipv6 & bytes)
inlineexplicitnoexcept

Constructor from byte array for IPv6.

Constructs an ip_address object with the specified byte array for an IPv6 address.

Parameters
[in]bytesA base_type_ipv6 object representing the byte array of the IPv6 address.

Member Function Documentation

◆ version()

constexpr inline ip_version version ( ) const
inlinenoexcept

Retrieves the version of the IP address.

Returns
The version of the IP address, either IPv4 or IPv6.

◆ is_multicast()

constexpr inline bool is_multicast ( ) const
inlinenoexcept

Checks if the IP address is a multicast address.

Returns
true if the IP address is reserved for multicast use, false otherwise.
See also
RFC 3171 for IPv4.
RFC 2373 for IPv6.

◆ is_private()

constexpr inline bool is_private ( ) const
inlinenoexcept

Checks if the IP address is a private address.

Returns
true if the IP address is allocated for private networks, false otherwise.
See also
iana-ipv4-special-registry.
iana-ipv6-special-registry.

◆ is_global()

constexpr inline bool is_global ( ) const
inlinenoexcept

Checks if the IP address is a global address.

Returns
true if the IP address is allocated for public networks, false otherwise.
See also
iana-ipv4-special-registry.
iana-ipv6-special-registry.

◆ is_reserved()

constexpr inline bool is_reserved ( ) const
inlinenoexcept

Checks if the IP address is a reserved address.

Returns
true if the IP address is otherwise IETF reserved, false otherwise.

◆ is_loopback()

constexpr inline bool is_loopback ( ) const
inlinenoexcept

Checks if the IP address is a loopback address.

Returns
true if the IP address is a loopback address, false otherwise.
See also
RFC 3330 for IPv4.
RFC 2373 for IPv6.

◆ is_link_local()

constexpr inline bool is_link_local ( ) const
inlinenoexcept

Checks if the IP address is link-local.

Returns
True if the IP address is link-local, false otherwise.

◆ is_unspecified()

constexpr inline bool is_unspecified ( ) const
inlinenoexcept

Checks if the IP address is unspecified.

An unspecified IP address is an address with all bits set to zero. In IPv4, this is represented as 0.0.0.0, and in IPv6, as ::. This type of address is used to indicate the absence of an address.

Returns
true if the IP address is unspecified (all bits are zero), false otherwise.
See also
RFC 5735 for IPv4.
RFC 2373 for IPv6.

◆ is_site_local()

constexpr inline bool is_site_local ( ) const
inlinenoexcept

Checks if the IPv6 address is a site-local address.

Returns
true if the address is site-local, false otherwise.
Note
Site-local addresses are equivalent to private addresses in IPv4 and are not routable on the global internet.
Remarks
These attribute is true for the network as a whole if it is true for both the network address and the broadcast address.

◆ is_v4()

constexpr inline bool is_v4 ( ) const
inlinenoexcept

Checks if the IP address is an IPv4 address.

Returns
true if the IP address is an IPv4 address, false otherwise.

◆ is_v6()

constexpr inline bool is_v6 ( ) const
inlinenoexcept

Checks if the IP address is an IPv6 address.

Returns
true if the IP address is an IPv6 address, false otherwise.

◆ size()

constexpr inline size_t size ( ) const
inlinenoexcept

Retrieves the size of the IP address.

Depending on the IP version, this function returns the size of the IPv4 or IPv6 address.

Returns
The size of the IP address in bytes.

◆ hash()

constexpr inline size_t hash ( ) const
inlinenoexcept

Computes a hash value for the IP address.

This function generates a hash value that can be used to uniquely identify the IP address. It can be useful when IP addresses are used as keys in hash tables.

Returns
A size_t hash value of the IP address.

◆ ipv4_mapped()

constexpr inline optional< ipv4_address > ipv4_mapped ( ) const
inlinenoexcept

Determines if the IPv6 address is an IPv4-mapped address.

If the IP address is an IPv6 address, this function returns the IPv4-mapped IPv6 address if available.

Returns
An optional containing the mapped IPv4 address if the IPv6 address is IPv4-mapped, or an empty optional otherwise.
Remarks
An IPv4-mapped IPv6 address has its first 80 bits set to zero and the next 16 bits set to one (starting with ::FFFF/96).

◆ sixtofour()

constexpr inline optional< ipv4_address > sixtofour ( ) const
inlinenoexcept

Determines if the IPv6 address is a 6to4 address.

If the IP address is an IPv6 address, this function returns the 6to4 address if available.

Returns
An optional containing the encapsulated IPv4 address if the IPv6 address is a 6to4 address, or an empty optional otherwise.
Remarks
A 6to4 address uses a 2002::/16 prefix and embeds an IPv4 address in the next 32 bits.
See also
RFC 3056.

◆ teredo()

constexpr inline optional< std::pair< ipv4_address, ipv4_address > > teredo ( ) const
inlinenoexcept

Determines if the IPv6 address is a Teredo address.

If the IP address is an IPv6 address, this function returns the Teredo address, which includes both the Teredo server and client IPv4 addresses.

Returns
An optional containing a pair of IPv4 addresses representing the Teredo server and client if the IPv6 address is a Teredo address, or an empty optional otherwise.
Return values
std::pair::firstThe Teredo server IPv4 address
std::pair::secondThe Teredo client IPv4 address
Remarks
A Teredo address begins with the 2001::/32 prefix and is used for NAT traversal for IPv6.
See also
RFC 4380.

◆ v4()

constexpr inline optional< ipv4_address > v4 ( ) const
inlinenoexcept

Retrieves the IPv4 address.

If the IP address is an IPv4 address, this function returns the IPv4 address.

Returns
An optional containing the IPv4 address, or an empty optional if the IP address is not IPv4.

◆ v6()

constexpr inline optional< ipv6_address > v6 ( ) const
inlinenoexcept

Retrieves the IPv6 address.

If the IP address is an IPv6 address, this function returns the IPv6 address.

Returns
An optional containing the IPv6 address, or an empty optional if the IP address is not IPv6.

◆ data()

constexpr inline const uint8_t * data ( ) const
inlinenoexcept

Retrieves the raw data of the IP address in network byte order (big-endian).

This function returns a pointer to the raw data representing the IP address. The format of the data depends on whether the address is IPv4 or IPv6.

Returns
A pointer to the raw data of the IP address.

◆ from_bytes() [1/3]

static constexpr inline ip_address from_bytes ( const base_type_ipv4 & bytes)
inlinestaticnoexcept

Static factory method to create an ip_address from byte array for IPv4.

Parameters
[in]bytesA base_type_ipv4 object representing the byte array of the IPv4 address.
Returns
An ip_address object initialized with the provided IPv4 bytes.

◆ from_bytes() [2/3]

static constexpr inline ip_address from_bytes ( const base_type_ipv6 & bytes)
inlinestaticnoexcept

Static factory method to create an ip_address from byte array for IPv6.

Parameters
[in]bytesA base_type_ipv6 object representing the byte array of the IPv6 address.
Returns
An ip_address object initialized with the provided IPv6 bytes.

◆ from_bytes() [3/3]

static constexpr inline ip_address from_bytes ( const uint8_t * bytes,
size_t byte_count,
ip_version version )
inlinestaticnoexcept

Static factory method to create an ip_address from raw bytes.

This method creates an ip_address object from raw bytes, with the specified IP version.

Parameters
[in]bytesA pointer to the raw bytes representing the IP address.
[in]byte_countThe number of bytes to use from the pointer.
[in]versionThe version of the IP address to create (IPv4 or IPv6).
Returns
An ip_address object initialized with the provided bytes and version.
Remarks
If the number of bytes byte_count is less than the target number . of bytes to represent the IP address, the missing bytes will be filled with zeros.
Remarks
If the number of bytes byte_count is greater than the target . number of bytes represented by the IP address, then the extra bytes will be ignored

◆ from_uint() [1/2]

static constexpr inline ip_address from_uint ( uint_type_ipv4 ip)
inlinestaticnoexcept

Static factory method to create an ip_address from a uint_type_ipv4.

Parameters
[in]ipA uint_type_ipv4 (uint32_t) representing the IPv4 address.
Returns
An ip_address object initialized with the provided IPv4 address.
Remarks
Bytes in integer ip must be presented in host byte order.

◆ from_uint() [2/2]

static constexpr inline ip_address from_uint ( const uint_type_ipv6 & ip)
inlinestaticnoexcept

Static factory method to create an ip_address from a uint_type_ipv6.

Parameters
[in]ipA uint_type_ipv6 (uint128_t) representing the IPv6 address.
Returns
An ip_address object initialized with the provided IPv6 address.
Remarks
Bytes in integer ip must be presented in host byte order.

◆ to_uint32()

constexpr inline uint32_t to_uint32 ( ) const
inlinenoexcept

Converts the IP address to a uint32_t.

This function returns the numeric representation of the IP address as a uint32_t. For IPv6 addresses, it returns the least significant 32 bits of the address.

Returns
A uint32_t representing the IPv4 address or the least significant 32 bits of the IPv6 address.
Remarks
Bytes in integer are presented in host byte order.

◆ to_uint128()

constexpr inline uint128_t to_uint128 ( ) const
inlinenoexcept

Converts the IP address to a uint128_t.

This function returns the numeric representation of the IP address as a uint128_t. For IPv4 addresses, the function converts the address to a uint128_t format.

Returns
A uint128_t representing the numeric value of the IP address.
Remarks
Bytes in integer are presented in host byte order.

◆ to_string()

inline std::string to_string ( format fmt = format::compressed) const
inline

Converts the IP address to a string.

This function returns a string representation of the IP address. The format can be specified by the fmt parameter.

Parameters
[in]fmtThe format to use for the string representation, defaults to compressed format.
Returns
A std::string representing the IP address in the specified format.

◆ to_wstring()

inline std::wstring to_wstring ( format fmt = format::compressed) const
inline

Converts the IP address to a string.

This function returns a string representation of the IP address. The format can be specified by the fmt parameter.

Parameters
[in]fmtThe format to use for the string representation, defaults to compressed format.
Returns
A std::wstring representing the IP address in the specified format.

◆ to_u16string()

inline std::u16string to_u16string ( format fmt = format::compressed) const
inline

Converts the IP address to a string.

This function returns a string representation of the IP address. The format can be specified by the fmt parameter.

Parameters
[in]fmtThe format to use for the string representation, defaults to compressed format.
Returns
A std::u16string representing the IP address in the specified format.

◆ to_u32string()

inline std::u32string to_u32string ( format fmt = format::compressed) const
inline

Converts the IP address to a string.

This function returns a string representation of the IP address. The format can be specified by the fmt parameter.

Parameters
[in]fmtThe format to use for the string representation, defaults to compressed format.
Returns
A std::u32string representing the IP address in the specified format.

◆ to_u8string()

inline std::u8string to_u8string ( format fmt = format::compressed) const
inline

Converts the IP address to a string.

This function returns a string representation of the IP address. The format can be specified by the fmt parameter.

Parameters
[in]fmtThe format to use for the string representation, defaults to compressed format.
Returns
A std::u8string representing the IP address in the specified format.

◆ reverse_pointer()

inline std::string reverse_pointer ( ) const
inline

Generates a reverse DNS lookup pointer for the IP address.

This function creates a string that is the reverse DNS lookup pointer of the IP address. It is commonly used in reverse DNS lookups, where the IP address is reversed and appended with .in-addr.arpa for IPv4, or .ip6.arpa for IPv6, to form a domain name that can be looked up in the DNS system.

The name of the reverse DNS PTR record for the IP address, e.g.:

std::cout << ip_address::parse("127.0.0.1").reverse_pointer() << std::endl;
std::cout << ip_address::parse("2001:db8::1").reverse_pointer() << std::endl;
// out:
// 1.0.0.127.in-addr.arpa
// 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
inline std::string reverse_pointer() const
Generates a reverse DNS lookup pointer for the IP address.
Definition ip-any-address.hpp:522
static consteval inline ip_address parse() noexcept
Static method template to parse an IP address from a fixed string at compile time.
Definition ip-any-address.hpp:550
Returns
A std::string that is the reverse DNS lookup pointer of the IP address.

◆ swap()

constexpr inline void swap ( ip_address & ip)
inlinenoexcept

Swaps the contents of this ip_address with another ip_address.

This function exchanges the contents of the ip_address with those of the ip parameter.

Parameters
[in]ipThe other ip_address object to swap with.

◆ parse() [1/13]

template<fixed_string FixedString>
static consteval inline ip_address parse ( )
inlinestaticnoexcept

Static method template to parse an IP address from a fixed string at compile time.

Template Parameters
FixedStringA non-type template parameter that holds the string representation of the IP address.
Returns
An instance of ip address parsed from the fixed string.
Note
This method is only available when non-type template parameters for strings are supported.
Remarks
If parsing fails, an error will be raised at compile time.

◆ parse() [2/13]

static constexpr inline ip_address parse ( std::string_view address)
inlinestatic

Parses an IP address from a string view.

This method provides a way to parse an IP address from a string view, which is a non-owning reference to a sequence of characters.

Parameters
[in]addressThe string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.

◆ parse() [3/13]

static constexpr inline ip_address parse ( std::wstring_view address)
inlinestatic

Parses an IP address from a wide string view.

This method allows for parsing an IP address from a wide string view, which is typically used for wide character strings.

Parameters
[in]addressThe wide string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.

◆ parse() [4/13]

static constexpr inline ip_address parse ( std::u8string_view address)
inlinestatic

Parses an IP address from a UTF-8 string view.

This method parses an IP address from a given UTF-8 string view. It leverages the char8_t type introduced in C++20 to handle UTF-8 strings natively.

Parameters
[in]addressA UTF-8 string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++20 and later versions where char8_t is supported.

◆ parse() [5/13]

static constexpr inline ip_address parse ( std::u16string_view address)
inlinestatic

Parses an IP address from a UTF-16 string view.

This method is designed to parse an IP address from a UTF-16 string view, which is a sequence of 16-bit characters.

Parameters
[in]addressThe UTF-16 string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.

◆ parse() [6/13]

static constexpr inline ip_address parse ( std::u32string_view address)
inlinestatic

Parses an IP address from a UTF-32 string view.

This method allows for parsing an IP address from a UTF-32 string view, which is a sequence of 32-bit characters.

Parameters
[in]addressThe UTF-32 string view containing the IP address to parse.
Returns
An instance of ip address parsed from the string view.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.

◆ parse() [7/13]

static constexpr inline ip_address parse ( std::string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a string view and reports errors through an error code.

This method parses an IP address from a string view and provides an error code if the parsing fails.

Parameters
[in]addressThe string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.

◆ parse() [8/13]

static constexpr inline ip_address parse ( std::wstring_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a wide string view and reports errors through an error code.

This method parses an IP address from a wide string view and provides an error code if the parsing fails.

Parameters
[in]addressThe wide string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the wide string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.

◆ parse() [9/13]

static constexpr inline ip_address parse ( std::u8string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a UTF-8 string view and reports errors through an error code.

This method parses an IP address from a UTF-8 string view and provides an error code if the parsing fails.

Parameters
[in]addressThe UTF-8 string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the UTF-8 string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++20 and later versions where char8_t is supported.

◆ parse() [10/13]

static constexpr inline ip_address parse ( std::u16string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a UTF-16 string view and reports errors through an error code.

This method parses an IP address from a UTF-16 string view and provides an error code if the parsing fails.

Parameters
[in]addressThe UTF-16 string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the UTF-16 string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.

◆ parse() [11/13]

static constexpr inline ip_address parse ( std::u32string_view address,
error_code & code )
inlinestaticnoexcept

Parses an IP address from a UTF-32 string view and reports errors through an error code.

This method parses an IP address from a UTF-32 string view and provides an error code if the parsing fails.

Parameters
[in]addressThe UTF-32 string view containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the UTF-32 string view. If parsing fails, the returned object will be in an unspecified state.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.

◆ set_scope_id() [1/11]

constexpr inline void set_scope_id ( std::string_view scope_id)
inline

Sets the scope identifier of the IPv6 address using a string view.

This function sets the scope identifier using a string view, which allows for a more efficient way to handle strings without copying them.

Parameters
[in]scope_idThe string view representing the scope identifier.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [2/11]

constexpr inline void set_scope_id ( std::wstring_view scope_id)
inline

Sets the scope identifier of the IPv6 address using a wide string view.

This function sets the scope identifier using a wide string view, which allows for a more efficient way to handle strings without copying them.

Parameters
[in]scope_idThe wide string view representing the scope identifier.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [3/11]

constexpr inline void set_scope_id ( std::u8string_view scope_id)
inline

Sets the scope identifier of the IPv6 address using UTF-8 string view.

This function sets the scope identifier using UTF-8 string view, which allows for a more efficient way to handle strings without copying them.

Parameters
[in]scope_idThe UTF-8 string view representing the scope identifier.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++20 and later versions where char8_t is supported.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [4/11]

constexpr inline void set_scope_id ( std::u16string_view scope_id)
inline

Sets the scope identifier of the IPv6 address using UTF-16 string view.

This function sets the scope identifier using UTF-16 string view, which allows for a more efficient way to handle strings without copying them.

Parameters
[in]scope_idThe UTF-16 string view representing the scope identifier.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [5/11]

constexpr inline void set_scope_id ( std::u32string_view scope_id)
inline

Sets the scope identifier of the IPv6 address using UTF-32 string view.

This function sets the scope identifier using UTF-32 string view, which allows for a more efficient way to handle strings without copying them.

Parameters
[in]scope_idThe UTF-32 string view representing the scope identifier.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [6/11]

constexpr inline void set_scope_id ( std::string_view scope_id,
error_code & code )
inlinenoexcept

Sets the scope identifier of the IPv6 address using a string view and reports any errors encountered.

This function sets the scope identifier using a string view and provides an error code parameter to report any issues that occur during the operation.

Parameters
[in]scope_idThe string view representing the scope identifier.
[out]codeAn error_code object that will be set to the error that occurred, if any.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [7/11]

constexpr inline void set_scope_id ( std::wstring_view scope_id,
error_code & code )
inlinenoexcept

Sets the scope identifier of the IPv6 address using a wide string view and reports any errors encountered.

This function sets the scope identifier using a wide string view and provides an error code parameter to report any issues that occur during the operation.

Parameters
[in]scope_idThe wide string view representing the scope identifier.
[out]codeAn error_code object that will be set to the error that occurred, if any.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [8/11]

constexpr inline void set_scope_id ( std::u8string_view scope_id,
error_code & code )
inlinenoexcept

Sets the scope identifier of the IPv6 address using a UTF-8 string view and reports any errors encountered.

This function sets the scope identifier using a UTF-8 string view and provides an error code parameter to report any issues that occur during the operation.

Parameters
[in]scope_idThe UTF-8 string view representing the scope identifier.
[out]codeAn error_code object that will be set to the error that occurred, if any.
Note
This method is available for C++20 and later versions.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [9/11]

constexpr inline void set_scope_id ( std::u16string_view scope_id,
error_code & code )
inlinenoexcept

Sets the scope identifier of the IPv6 address using a UTF-16 string view and reports any errors encountered.

This function sets the scope identifier using a UTF-16 string view and provides an error code parameter to report any issues that occur during the operation.

Parameters
[in]scope_idThe UTF-16 string view representing the scope identifier.
[out]codeAn error_code object that will be set to the error that occurred, if any.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ set_scope_id() [10/11]

constexpr inline void set_scope_id ( std::u32string_view scope_id,
error_code & code )
inlinenoexcept

Sets the scope identifier of the IPv6 address using a UTF-32 string view and reports any errors encountered.

This function sets the scope identifier using a UTF-32 string view and provides an error code parameter to report any issues that occur during the operation.

Parameters
[in]scope_idThe UTF-32 string view representing the scope identifier.
[out]codeAn error_code object that will be set to the error that occurred, if any.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ parse() [12/13]

template<typename T , size_t N>
static constexpr inline ip_address parse ( const T(&) address[N])
inlinestatic

Parses an IP address from a character array.

This method template parses an IP address from a character array of a specified size. Can check and get the result at compile time.

Template Parameters
TThe character type of the array.
NThe size of the character array.
Parameters
[in]addressThe character array containing the IP address to parse.
Returns
An instance of ip address parsed from the character array.

◆ parse() [13/13]

template<typename T , size_t N>
static constexpr inline ip_address parse ( const T(&) address[N],
error_code & code )
inlinestaticnoexcept

Parses an IP address from a character array and reports errors through an error code.

This method template parses an IP address from a character array of a specified size and provides an error code if the parsing fails.

Template Parameters
TThe character type of the array.
NThe size of the character array.
Parameters
[in]addressThe character array containing the IP address to parse.
[out]codeA reference to an error_code object that will be set if an error occurs during parsing.
Returns
An instance of ip address parsed from the character array. If parsing fails, the returned object will be in an unspecified state.

◆ set_scope_id() [11/11]

template<typename T , size_t N>
constexpr inline void set_scope_id ( const T(&) scope_id[N])
inlinenoexcept

Sets the scope identifier of the IPv6 address.

This function sets the scope identifier using a character array. The length of the array should not exceed IPADDRESS_IPV6_SCOPE_MAX_LENGTH + 1.

Template Parameters
TThe character type of the scope identifier.
NThe size of the scope identifier array.
Parameters
[in]scope_idThe character array representing the scope identifier.
Remarks
If scope is disabled in settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0) then this call will have no effect.

◆ get_scope_id()

constexpr inline scope get_scope_id ( ) const
inlinenoexcept

Retrieves the scope identifier of the IPv6 address.

The scope identifier is used to determine the context in which the address is valid. It is particularly relevant for link-local and site-local addresses.

Returns
A scope object representing the scope identifier of the IPv6 address.
Remarks
If the scope is disabled in the settings (IPADDRESS_IPV6_SCOPE_MAX_LENGTH == 0), then an empty scope will be returned.

◆ operator uint32_t()

constexpr inline operator uint32_t ( ) const
inlineexplicitnoexcept

Converts the IP address to a uint32_t.

This function casts the IP address to a uint32_t. For IPv4 addresses, it returns the direct numeric representation. For IPv6 addresses, it returns the least significant 32 bits of the address.

Returns
A uint32_t representing the IPv4 address or the least significant 32 bits of the IPv6 address.
Remarks
Bytes in integer are presented in host byte order.

◆ operator uint128_t()

constexpr inline operator uint128_t ( ) const
inlineexplicitnoexcept

Converts the IP address to a uint128_t.

This function casts the IP address to a uint128_t. It is used to obtain the full numeric representation of the IP address, especially for IPv6 addresses.

Returns
A uint128_t representing the numeric value of the IP address.
Remarks
Bytes in integer are presented in host byte order.

◆ operator std::basic_string< T, std::char_traits< T >, std::allocator< T > >()

template<typename T >
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > > ( ) const
inlineexplicit

Converts the IP address to a string.

This operator allows the IP address to be converted to a string.

Template Parameters
TThe character type of the string.
Returns
A std::string representation of the IP address.

◆ operator==()

constexpr inline bool operator== ( const ip_address & rhs) const
inlinenoexcept

Equality comparison operator.

Compares this IP address with another IP address for equality.

Parameters
[in]rhsThe right-hand side ip_address object for comparison.
Returns
True if both IP addresses are equal, false otherwise.

◆ operator!=()

constexpr inline bool operator!= ( const ip_address & rhs) const
inlinenoexcept

Inequality comparison operator.

Compares this IP address with another IP address for inequality.

Parameters
[in]rhsThe right-hand side ip_address object for comparison.
Returns
True if both IP addresses are not equal, false otherwise.

◆ operator<()

constexpr inline bool operator< ( const ip_address & rhs) const
inlinenoexcept

Less than comparison operator.

Compares this IP address with another IP address to determine if it is less than the other.

Parameters
[in]rhsThe right-hand side ip_address object for comparison.
Returns
True if this IP address is less than the other, false otherwise.

◆ operator>()

constexpr inline bool operator> ( const ip_address & rhs) const
inlinenoexcept

Greater than comparison operator.

Compares this IP address with another IP address to determine if it is greater than the other.

Parameters
[in]rhsThe right-hand side ip_address object for comparison.
Returns
True if this IP address is greater than the other, false otherwise.

◆ operator<=()

constexpr inline bool operator<= ( const ip_address & rhs) const
inlinenoexcept

Less than or equal to comparison operator.

Compares this IP address with another IP address to determine if it is less than or equal to the other.

Parameters
[in]rhsThe right-hand side ip_address object for comparison.
Returns
True if this IP address is less than or equal to the other, false otherwise.

◆ operator>=()

constexpr inline bool operator>= ( const ip_address & rhs) const
inlinenoexcept

Greater than or equal to comparison operator.

Compares this IP address with another IP address to determine if it is greater than or equal to the other.

Parameters
[in]rhsThe right-hand side ip_address object for comparison.
Returns
True if this IP address is greater than or equal to the other, false otherwise.

The documentation for this class was generated from the following file: