[Bf-blender-cvs] [5c5ec837b3a] master: Utils: Add macro for C++ default arguments in C headers

Julian Eisel noreply at git.blender.org
Tue Apr 26 15:47:32 CEST 2022


Commit: 5c5ec837b3a565da8647444b807091a78df40fd9
Author: Julian Eisel
Date:   Tue Apr 26 15:13:58 2022 +0200
Branches: master
https://developer.blender.org/rB5c5ec837b3a565da8647444b807091a78df40fd9

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

===================================================================

M	source/blender/blenlib/BLI_utildefines.h

===================================================================

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
+
 /** \} */
 
 /* -------------------------------------------------------------------- */



More information about the Bf-blender-cvs mailing list