diff --git a/src/util/OptionalField.hxx b/src/util/OptionalField.hxx new file mode 100644 index 000000000..ec32807a0 --- /dev/null +++ b/src/util/OptionalField.hxx @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#pragma once + +#include // 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 struct OptionalField; + +template +struct OptionalField +{ + template + constexpr OptionalField(Args&&...) {} +}; + +template +struct OptionalField +{ + T value; + + template + constexpr OptionalField(Args&&... args) + :value(std::forward(args)...) {} +};