From 5c5ec837b3a565da8647444b807091a78df40fd9 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Tue, 26 Apr 2022 15:13:58 +0200 Subject: Utils: Add macro for C++ default arguments in C headers This macro allows defining a default argument for when the translation unit is compiled in C++. Otherwise (in C), the argument has to be passed explicitly. A couple of benefits: * Default arguments are a nice quality-of-life feature in C++. It's annoying if these can't be used in C++ files, just because the header with the function declaration still needs to be C compatible. * Adds useful information to the API declaration. E.g. that an argument can be nullptr. * Should help us to move to using default arguments more, helping readability (arguably) Used in D14653. Reviewed By: LazyDodo Differential Revision: https://developer.blender.org/D14654 --- source/blender/blenlib/BLI_utildefines.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 51f0d3f486a..b8407a5453f 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -812,6 +812,16 @@ extern bool BLI_memory_is_zero(const void *arr, size_t arr_size); # define ENUM_OPERATORS(_type, _max) #endif +/** + * Utility so function declarations in C headers can use C++ default arguments. The default is then + * available when included in a C++ file, otherwise the argument has to be set explicitly. + */ +#ifdef __cplusplus +# define CPP_ARG_DEFAULT(default_value) = default_value +#else +# define CPP_ARG_DEFAULT(default_value) +#endif + /** \} */ /* -------------------------------------------------------------------- */ -- cgit v1.2.3