JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
reserve.hpp
Go to the documentation of this file.
1/// \file jsonv/detail/reserve.hpp
2/// Definition of the \c reserve_if_possible utility.
3///
4/// Copyright (c) 2026 by Travis Gockel. All rights reserved.
5///
6/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8/// version.
9///
10/// \author Travis Gockel (travis@gockelhut.com)
11#pragma once
12
13#include <jsonv/config.hpp>
14
15namespace jsonv::detail
16{
17
18/// Call \c reserve on \a container with the given \a capacity if it has such a member function; do nothing if it does
19/// not.
20///
21/// This exists because the containers an \c adapter fills are not all node-based or all contiguous: \c std::vector can
22/// be sized once from a count known up front, while \c std::list and \c std::set have nothing to size.
23template <typename TContainer, typename TCapacity>
24void reserve_if_possible(TContainer& container, const TCapacity& capacity)
25{
26 if constexpr (requires { container.reserve(capacity); })
27 container.reserve(capacity);
28}
29
30}
Copyright (c) 2014-2020 by Travis Gockel.