21 Internet protocol [internet]

21.4 Class ip::address [internet.address]

The class address is a version-independent representation for an IP address. An object of class address holds either an IPv4 address, an IPv6 address, or no valid address.

namespace std {
namespace experimental {
namespace net {
inline namespace v1 {
namespace ip {

  class address
  {
  public:
    // [internet.address.cons], constructors:
    constexpr address() noexcept;
    constexpr address(const address& a) noexcept;
    constexpr address(const address_v4& a) noexcept;
    constexpr address(const address_v6& a) noexcept;

    // [internet.address.assign], assignment:
    address& operator=(const address& a) noexcept;
    address& operator=(const address_v4& a) noexcept;
    address& operator=(const address_v6& a) noexcept;

    // [internet.address.members], members:
    constexpr bool is_v4() const noexcept;
    constexpr bool is_v6() const noexcept;
    constexpr address_v4 to_v4() const;
    constexpr address_v6 to_v6() const;
    constexpr bool is_unspecified() const noexcept;
    constexpr bool is_loopback() const noexcept;
    constexpr bool is_multicast() const noexcept;
    template<class Allocator = allocator<char>>
      basic_string<char, char_traits<char>, Allocator>
        to_string(const Allocator& a = Allocator()) const;

  private:
    address_v4 v4_; // exposition only
    address_v6 v6_; // exposition only
  };

  // [internet.address.comparisons], address comparisons:
  constexpr bool operator==(const address& a, const address& b) noexcept;
  constexpr bool operator!=(const address& a, const address& b) noexcept;
  constexpr bool operator< (const address& a, const address& b) noexcept;
  constexpr bool operator> (const address& a, const address& b) noexcept;
  constexpr bool operator<=(const address& a, const address& b) noexcept;
  constexpr bool operator>=(const address& a, const address& b) noexcept;

  // [internet.address.creation], address creation:
  address make_address(const char* str);
  address make_address(const char* str, error_code& ec) noexcept;
  address make_address(const string& str);
  address make_address(const string& str, error_code& ec) noexcept;
  address make_address(string_view str);
  address make_address(string_view str, error_code& ec) noexcept;

  // [internet.address.io], address I/O:
  template<class CharT, class Traits>
    basic_ostream<CharT, Traits>& operator<<(
      basic_ostream<CharT, Traits>& os, const address& addr);

} // namespace ip
} // inline namespace v1
} // namespace net
} // namespace experimental
} // namespace std

address satisfies the requirements for Destructible (C++ 2014 [destructible]), CopyConstructible (C++ 2014 [copyconstructible]), and CopyAssignable (C++ 2014 [copyassignable]).

21.4.1 ip::address constructors [internet.address.cons]

constexpr address() noexcept;

Postconditions: is_v4() == true, is_v6() == false, and is_unspecified() == true.

constexpr address(const address_v4& a) noexcept;

Effects: Initializes v4_ with a.

Postconditions: is_v4() == true and is_v6() == false.

constexpr address(const address_v6& a) noexcept;

Effects: Initializes v6_ with a.

Postconditions: is_v4() == false and is_v6() == true.

21.4.2 ip::address assignment [internet.address.assign]

address& operator=(const address_v4& a) noexcept;

Postconditions: is_v4() == true and is_v6() == false and to_v4() == a.

Returns: *this

address& operator=(const address_v6& a) noexcept;

Postconditions: is_v4() == false and is_v6() == true and to_v6() == a.

Returns: *this

21.4.3 ip::address members [internet.address.members]

constexpr bool is_v4() const noexcept;

Returns: true if the object contains an IP version 4 address, otherwise false.

constexpr bool is_v6() const noexcept;

Returns: true if the object contains an IP version 6 address, otherwise false.

constexpr address_v4 to_v4() const;

Returns: v4_.

Remarks: bad_address_cast if is_v4() == false.

constexpr address_v6 to_v6() const;

Returns: v6_.

Remarks: bad_address_cast if is_v6() == false.

constexpr bool is_unspecified() const noexcept;

Returns: If is_v4(), returns v4_.is_unspecified(). Otherwise returns v6_.is_unspecified().

constexpr bool is_loopback() const noexcept;

Returns: If is_v4(), returns v4_.is_loopback(). Otherwise returns v6_.is_loopback().

constexpr bool is_multicast() const noexcept;

Returns: If is_v4(), returns v4_.is_multicast(). Otherwise returns v6_.is_multicast().

template<class Allocator = allocator<char>> basic_string<char, char_traits<char>, Allocator> to_string(const Allocator& a = Allocator()) const;

Returns: If is_v4(), returns v4_.to_string(a). Otherwise returns v6_.to_string(a).

21.4.4 ip::address comparisons [internet.address.comparisons]

constexpr bool operator==(const address& a, const address& b) noexcept;

Returns:

  • if a.is_v4() != b.is_v4(), false;

  • if a.is_v4(), the result of a.v4_ == b.v4_;

  • otherwise, the result of a.v6_ == b.v6_.

constexpr bool operator!=(const address& a, const address& b) noexcept;

Returns: !(a == b).

constexpr bool operator< (const address& a, const address& b) noexcept;

Returns:

  • if a.is_v4() && !b.is_v4(), true;

  • if !a.is_v4() && b.is_v4(), false;

  • if a.is_v4(), the result of a.v4_ < b.v4_;

  • otherwise, the result of a.v6_ < b.v6_.

constexpr bool operator> (const address& a, const address& b) noexcept;

Returns: b < a.

constexpr bool operator<=(const address& a, const address& b) noexcept;

Returns: !(b < a).

constexpr bool operator>=(const address& a, const address& b) noexcept;

Returns: !(a < b).

21.4.5 ip::address creation [internet.address.creation]

address make_address(const char* str); address make_address(const char* str, error_code& ec) noexcept; address make_address(const string& str); address make_address(const string& str, error_code& ec) noexcept; address make_address(string_view str); address make_address(string_view str, error_code& ec) noexcept;

Effects: Converts a textual representation of an address into an object of class address, as if by calling:

address a;
address_v6 v6a = make_address_v6(str, ec);
if (!ec)
  a = v6a;
else{
  address_v4 v4a = make_address_v4(str, ec);
  if (!ec)
    a = v4a;
}

Returns: a.

21.4.6 ip::address I/O [internet.address.io]

template<class CharT, class Traits> basic_ostream<CharT, Traits>& operator<<( basic_ostream<CharT, Traits>& os, const address& addr);

Returns: os << addr.to_string().c_str().