<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Aug 20, 2017 at 1:50 PM, Aleksander Morgado <span dir="ltr"><<a href="mailto:aleksander@aleksander.es" target="_blank">aleksander@aleksander.es</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On Sun, Aug 20, 2017 at 7:09 PM, Ben Chan <<a href="mailto:benchan@chromium.org">benchan@chromium.org</a>> wrote:<br>
><br>
><br>
> On Sun, Aug 20, 2017 at 9:37 AM, Aleksander Morgado<br>
> <<a href="mailto:aleksander@aleksander.es">aleksander@aleksander.es</a>> wrote:<br>
>><br>
>> Using an intermediate constant variable breaks compilation with C<br>
>> compilers, as these variables cannot be used as initializers.<br>
>><br>
><br>
> Aleksander, do you have the compiler version and the code snippet that<br>
> causes the compiler error?<br>
><br>
<br>
</span>Here's an example:<br>
<br>
/* test.c */<br>
#include <stdio.h><br>
#include <ModemManager.h><br>
<br>
typedef struct {<br>
MMModemBand band;<br>
char *str;<br>
} BandName;<br>
<br>
static const BandName names[] = {<br>
{ .band = MM_MODEM_BAND_EUTRAN_XLII, "e-utran 43" },<br>
};<br>
<br>
int main(int argc, char **argv)<br>
{<br>
guint i;<br>
<br>
for (i = 0; i < G_N_ELEMENTS (names); i++)<br>
printf ("band '%s' has id %u\n", names[i].str, names[i].band);<br>
return 0;<br>
}<br>
<br>
Compilation fails with "error: initializer element is not constant":<br>
$ gcc -o test `pkg-config --cflags --libs ModemManager glib-2.0` test.c<br>
test.c:12:2: warning: ‘MM_DEPRECATED_MODEM_BAND_<wbr>EUTRAN_XLII’ is<br>
deprecated: Use 'MM_MODEM_BAND_EUTRAN_42' instead<br>
[-Wdeprecated-declarations]<br>
{ .band = MM_MODEM_BAND_EUTRAN_XLII, "e-utran 43" },<br>
^<br>
In file included from /usr/include/ModemManager/<wbr>ModemManager.h:41:0,<br>
from test.c:4:<br>
/usr/include/ModemManager/<wbr>ModemManager-compat.h:441:18: note: declared here<br>
<span class="gmail-"> static const int MM_DEPRECATED_MODEM_BAND_<wbr>EUTRAN_XLII =<br>
MM_MODEM_BAND_EUTRAN_42;<br>
</span> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<wbr>~~~~~~<br>
/usr/include/ModemManager/<wbr>ModemManager-compat.h:442:35: error:<br>
initializer element is not constant<br>
#define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_<wbr>EUTRAN_XLII<br>
^<br>
/usr/include/ModemManager/<wbr>ModemManager-compat.h:442:35: note: in<br>
definition of macro ‘MM_MODEM_BAND_EUTRAN_XLII’<br>
#define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_<wbr>EUTRAN_XLII<br>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<wbr>~~~~~~<br>
/usr/include/ModemManager/<wbr>ModemManager-compat.h:442:35: note: (near<br>
initialization for ‘names[0].band’)<br>
#define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_<wbr>EUTRAN_XLII<br>
^<br>
/usr/include/ModemManager/<wbr>ModemManager-compat.h:442:35: note: in<br>
definition of macro ‘MM_MODEM_BAND_EUTRAN_XLII’<br>
#define MM_MODEM_BAND_EUTRAN_XLII MM_DEPRECATED_MODEM_BAND_<wbr>EUTRAN_XLII<br>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<wbr>~~~~~~<br>
<br>
$ gcc --version<br>
gcc (GCC) 7.1.1 20170630<br>
<br>
For what I could gather browsing; using constant variables as<br>
initializers is valid C++, but not C. The approach I took for the<br>
patch is what the GTK+ did when deprecating GtkStock macros (e.g.<br>
<a href="https://github.com/GNOME/gtk/blob/7173df1dd1a8f45c47b75d30bfbfaa67f33d8e9b/gtk/deprecated/gtkstock.h#L102" rel="noreferrer" target="_blank">https://github.com/GNOME/gtk/<wbr>blob/<wbr>7173df1dd1a8f45c47b75d30bfbfaa<wbr>67f33d8e9b/gtk/deprecated/<wbr>gtkstock.h#L102</a>)<br></blockquote><div><br></div><div>lgtm.</div><div><br></div><div>Yeah, unfortunately, it seems like C has a different rule for constant expressions (than C++). A simple expression like this fails to compile with gcc (even in c99, c11), but succeeds with clang:</div><div><br></div><div> static const int a = 1;</div><div> static const int b = a; /* error: initializer element is not constant */</div><div><br></div><div>Seems a bit subtle that `static const int a = 1` isn't regarded as a constant expression. It may be related to whether there is any guarantee on the initialization order.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<span class="gmail-HOEnZb"><font color="#888888"><br>
--<br>
Aleksander<br>
<a href="https://aleksander.es" rel="noreferrer" target="_blank">https://aleksander.es</a><br>
</font></span></blockquote></div><br></div></div>