[Spice-devel] [PATCH v2 05/13] Rephrase section about constants
Frediano Ziglio
fziglio at redhat.com
Thu Feb 8 08:48:46 UTC 2018
>
> From: Christophe de Dinechin <dinechin at redhat.com>
>
> The indent of the rephrasing is that:
>
> - If you have a single constant, use const, e.g. (visible in debugger)
> const unsigned max_stuff = 42;
>
> - If you have multiple constants, prefer enums over #define, as
> already suggested later in the guide:
> enum {
> A = 0,
> B = 42,
> C = -1
> }
>
The const is a bit different from C and C++ and this results
in a bit of confusion. max_stuff above is a compile time
constant in C++, not in C while the enumeration is always
a compile time constant in both languages.
So the choice is not only syntax one.
I think that the "Alternatively, use global `const` variables."
is quite an ancient comment when spice-server was in C++
(much before I join and I think even older than the git repository!)
Unfortunately also C currently can't attach a type to the
enumeration values so if you want to define in C a constant
with a type usually you use a define like
#define MY_CONSTANT ((uint64_t)123)
You are right, this paragraph needs some care. Maybe the
style should be different for C and C++ and here we should
just use enum and #define while having a section for C++
telling about const ?
> Signed-off-by: Christophe de Dinechin <dinechin at redhat.com>
> ---
> docs/spice_style.txt | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/docs/spice_style.txt b/docs/spice_style.txt
> index 61cb0701..3e463d2f 100644
> --- a/docs/spice_style.txt
> +++ b/docs/spice_style.txt
> @@ -106,7 +106,9 @@ Using goto is allowed in C code for error handling. In
> any other case it will be
> Defining Constant values
> ------------------------
>
> -Use defines for constant values for improving readability and ease of
> changes. Alternatively, use global `const` variables.
> +Use defines for constant values for improving readability and ease of
> changes.
> +Alternatively, use global `const` variables for individual values.
> +If multiple related constants are to be defined, consider the use of
> enumerations with initializers.
>
> Short functions
> ---------------
Frediano
More information about the Spice-devel
mailing list