util/OptionalField: new library

This commit is contained in:
Max Kellermann 2023-09-12 20:32:13 +02:00 committed by Max Kellermann
parent 9fe813e572
commit 64b0587e78
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <mk@cm4all.com>
#pragma once
#include <utility> // for std::forward()
/**
* Helps with declaring a field that is present only under a certain
* (compile-time) condition. If `enable` is true, this struct
* contains a field named `value` of the specified type. To avoid
* memory overhead when disabled, add the attribute
* [[no_unique_address]].
*/
template<typename T, bool enable> struct OptionalField;
template<typename T>
struct OptionalField<T, false>
{
template<typename... Args>
constexpr OptionalField(Args&&...) {}
};
template<typename T>
struct OptionalField<T, true>
{
T value;
template<typename... Args>
constexpr OptionalField(Args&&... args)
:value(std::forward<Args>(args)...) {}
};