[Spice-devel] [PATCH v2 05/13] Rephrase section about constants
Christophe de Dinechin
christophe.de.dinechin at gmail.com
Thu Feb 8 11:30:31 UTC 2018
> On 8 Feb 2018, at 09:48, Frediano Ziglio <fziglio at redhat.com> wrote:
>
>>
>> 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 ?
Well, I was also considering debuggability.
Until recently, both gdb and lldb were easily confused by enums, whereas const variables tend to work better.
Christophe
>
>> 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
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel
More information about the Spice-devel
mailing list