Files
json/api/basic_json_view/basic_json_view/index.md
T
2026-10-21 10:31:00 +00:00

2.2 KiB

nlohmann::basic_json_view::basic_json_view

basic_json_view() noexcept = default;

Creates an invalid (discarded) view: type() is value_t::discarded and is_discarded() is true.

This is the only constructor a caller can use directly. Every other view is obtained from a basic_json_document, via root() or by navigating into a container with operator[], at, front, back, find, or iteration.

Exception safety

No-throw guarantee: this constructor never throws exceptions.

Complexity

Constant.

Notes

basic_json_view is trivially copyable (it holds two pointers), so a default-constructed view can be used as a placeholder for "no value yet" and later be assigned a real view.

Examples

Example

The example below shows the default constructor and that a basic_json_view is a small, copyable handle.

#include <iostream>
#include <nlohmann/json_view.hpp>

int main()
{
    std::cout << std::boolalpha;

    // the default constructor is the only public one: it creates an invalid
    // (discarded) view, useful as a "no value yet" placeholder
    nlohmann::json_view v;
    std::cout << v.is_discarded() << '\n';

    // views are trivially copyable handles (two pointers); the document owns
    // the actual data
    nlohmann::json_view copy = v;
    std::cout << copy.is_discarded() << '\n';
}

Output:

true
true

See also

  • is_discarded - return whether the view is invalid
  • root - the view of a document's root value

Version history

  • Added in version 3.13.0 unreleased.