skip to content

For a JSON value type in C++, when should you model it as a std::variant instead of a virtual class hierarchy, and what does each choice cost?

level: seniorimportance: should knowfreq 42%

answer

  1. a closed set of kinds
  2. new operations versus new types
  3. values, not pointers
  4. recursion needs a container

basics

~20 s

Use std::variant when the set of kinds is closed, like JSON's six kinds: values live in place without per-node heap allocation, and visitation flags missing cases. Use a virtual hierarchy when new kinds must be added independently.

solid answer

~50 s

JSON has a fixed set of six kinds, which is exactly what `std::variant` models well. Each value is stored in place with no per-node heap allocation and copies as a value; containers of them are contiguous; dispatch with `std::visit` is constant time; and adding a new **operation** is just a new visitor. Adding a new **kind** is the expensive direction: every exhaustive visit must change, which the compiler enforces, and that is a feature when the set is closed. A virtual hierarchy inverts the trade: new kinds can come from other modules or plugins without recompiling existing code, but each node is usually a heap object behind a `std::unique_ptr`, copying needs a `clone()`, and a new operation means touching every class. Variant costs size, since every node is as large as the largest alternative plus a tag, and recursion needs an indirection such as `std::vector<Json>`, because a variant cannot contain itself directly.

code

cpp · 28 lines
cpp
#include <iostream>
#include <string>
#include <utility>
#include <variant>
#include <vector>

template <class... Fs> struct overloaded : Fs... { using Fs::operator()...; };

struct Json;
using Array = std::vector<Json>;                       // vector allows an incomplete element type
using Object = std::vector<std::pair<std::string, Json>>;

struct Json {
    std::variant<std::nullptr_t, bool, double, std::string, Array, Object> v;
};

std::size_t count_leaves(const Json& j) {
    return std::visit(overloaded{
        [](const Array& a) { std::size_t n = 0; for (const auto& e : a) n += count_leaves(e); return n; },
        [](const Object& o) { std::size_t n = 0; for (const auto& [k, e] : o) n += count_leaves(e); return n; },
        [](const auto&) -> std::size_t { return 1; },  // null, bool, number, string
    }, j.v);
}

int main() {
    Json doc{Object{{"id", Json{42.0}}, {"tags", Json{Array{Json{std::string("a")}, Json{true}}}}}};
    std::cout << count_leaves(doc) << '\n';
}

go deeper

for a junior

Know that a variant lists its possible types up front, while a class hierarchy lets anyone add a derived class later.

for a middle

Compare storage and copying: a variant holds its value in place and copies as a value, while a hierarchy usually needs heap nodes and a clone function.

for a senior

Argue from the direction of change and the costs: closed sets and frequent new operations favour variant; open extension and stable binary interfaces favour a hierarchy. Mention size and recursion.

for a principal

Decide per domain model which extension axis the codebase must keep cheap, and set guidance on oversized alternatives and exhaustive visitation.

## Two ways to say "one of several kinds" C++ offers two standard tools for a value that can be one of several kinds: - a **closed sum type**, `std::variant<A, B, C>`, where the list of alternatives is part of the type; and - an **open hierarchy**, an abstract base class with virtual functions, where any code can add a derived class later. The general Visitor pattern and how virtual dispatch works are covered elsewhere; the question here is what C++ makes each choice cost for a concrete type like a JSON value or a network message. ## JSON as a variant JSON has exactly six kinds of value: null, boolean, number, string, array and object. That set has been stable for decades, so a closed type fits: ```cpp struct Json; using Array = std::vector<Json>; using Object = std::vector<std::pair<std::string, Json>>; struct Json { std::variant<std::nullptr_t, bool, double, std::string, Array, Object> v; }; ``` Two C++ details make this work: 1. A variant **cannot contain itself**, so the recursion goes through a wrapper struct and a container. 2. `std::vector` (like `std::list` and `std::forward_list`) is guaranteed since C++17 to accept an **incomplete element type** at instantiation, so `std::vector<Json>` can be named before `Json` is complete. The ordered and unordered maps carry no such guarantee in the draft, which is why the example stores an object's members as a vector of pairs. ## What each choice costs | Concern | `std::variant` | Virtual hierarchy | |---|---|---| | Set of kinds | closed, listed in the type | open, extensible from other modules | | Storage | in place, at least the largest alternative plus a tag | usually one heap object per node behind a `std::unique_ptr` | | Copying | value semantics for free | needs a virtual `clone()` | | Adding an operation | write one more visitor | add a virtual function to every class | | Adding a kind | change the type and every exhaustive visit | add one derived class | | Missing case | compile error with an exact-type visitor | a pure virtual function forces an override | | Dispatch | constant time for one variant, typically a jump table | an indirect call, typically through a vtable | | Binary interface | changing alternatives changes layout | new derived classes leave existing layout alone | The size row is the one teams underestimate. Every `Json` here is at least as large as its largest alternative, a `std::string` or a `std::vector`, plus the discriminator, even when it holds a `bool`. For a parse tree with millions of numbers that is real memory; a hierarchy pays a pointer per node plus a heap block sized to the actual kind. ## Network messages: the same question with a twist A protocol's message set looks closed, but versions add messages. A `std::variant<Ping, Data, Close>` makes every handler fail to compile when `Ack` is added, which is exactly what you want inside one service. If third-party plugins must handle messages the core does not know about, a hierarchy (or a type-erased handler table) fits better, because a variant's alternatives must be known where it is declared. ## Rules of thumb - **Choose variant** when the kinds are few, known and owned by one team; when values are copied, stored in containers or sent between threads; and when new operations arrive more often than new kinds. - **Choose a hierarchy** when kinds come from elsewhere (plugins, other libraries), when each kind is large and lives long behind a pointer anyway, or when a stable binary interface must allow new kinds. - **Watch the size** of the largest alternative; put a rare huge alternative behind a `std::unique_ptr` inside the variant to keep every other node small. - **Make visitation exhaustive** by default, with exact-type lambdas rather than catch-alls, so the compiler finds every site when the list changes. A strong answer names the direction of change as the deciding question: which will this codebase add more often, kinds or operations?

  • Why can't you write using Json = std::variant<std::nullptr_t, bool, double, std::string, std::vector<Json>>;?
    An alias cannot refer to itself, and `std::variant` needs complete alternative types to size its storage, so a variant cannot contain itself directly. The usual fix is a wrapper `struct Json` holding the variant, with recursion going through a container that accepts incomplete element types, such as `std::vector<Json>`, or through a `std::unique_ptr<Json>`.
  • One alternative of a message variant is a 4 KB buffer that is rarely used. What do you do?
    Every variant object is at least as large as its largest alternative, so the rare buffer would inflate every message. Store it indirectly, for example as a `std::unique_ptr<Buffer>` or a `std::vector<std::byte>` alternative, so the variant holds only a pointer-sized handle for that kind and the common small messages stay small.

  • std::variant allocates each alternative on the heap like a polymorphic object
  • A variant can hold a vector of itself directly without a wrapper type
  • Variant and a virtual hierarchy are equally easy to extend with new kinds
  • A variant's size depends on which alternative it currently holds
  • Visiting a variant is always slower than a virtual call