std::basic_..._view::empty(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::basic_string_view::empty β€” std::basic_string_view::empty

okay so welcome to the C++ Standard Libary manual, bestie β€” this is the std::basic_..._view::empty(3) page, and it's literally all about checking if a string view is empty. C++17 girlies, this one's for you.


🌸 Synopsis 🌸

constexpr bool empty() const noexcept;                (since C++17)
                                                        (until C++20)
[[nodiscard]] constexpr bool empty() const noexcept;  (since C++20)

This is the vibe check for the view: it checks if the view has no characters, i.e. whether size() == 0. No cap.

The plain constexpr cut is the since C++17 / until C++20 era; C++20 onward adds [[nodiscard]]. Periodt.


πŸŽ€ Parameters πŸŽ€

(none) β€” literally zero parameters. The function takes nothing, bestie.


🫢 Return value 🫢

true if the view is empty, false otherwise. That's the whole tea. β˜•


⚑ Complexity ⚑

Constant. Periodt. That's it.


🧚 Example 🧚

okay so here's the whole demo, exactly as it is:

// Run this code

#include <iostream>
#include <string_view>

// Print a string surrounded by single quotes, its
// length and whether it is considered empty.
void check_string(std::string_view ref)
{
    std::cout << std::boolalpha
              << "'" << ref << "' has " << ref.size()
              << " character(s); emptiness: " << ref.empty() << '\n';
}

int main(int argc, char **argv)
{
    // An empty string
    check_string("");

    // Almost always not empty: argv[0]
    if (argc > 0)
        check_string(argv[0]);
}

🌈 Possible output 🌈

Possible output, slay:

'' has 0 character(s); emptiness: true
'./a.out' has 7 character(s); emptiness: false

πŸ”₯ See also πŸ”₯

the cross-reference corner is a little chaotic, but every line is here:

  • size β€” returns the number of characters
  • length β€” (public member function)
  • max_size β€” returns the maximum number of characters (public member function)
  • size
  • ssize β€” returns the size of a container or array
  • (C++17) β€” (function template)
  • (C++20)
  • empty β€” checks whether the container is empty
  • (C++17) β€” (function template)
  • checks whether the string is empty
  • empty β€” (public member function of std::basic_string<CharT,Traits,Allocator>)

shoutout to source/version http://cppreference.com and the date stamp 2024.06.10 β€” this was the std::basic_..._view::empty(3) page, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from