Sanity check - making a static initializer constant
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Oct 10 13:50:00 GMT 2013
On 9 October 2013 20:10, Ian Pilcher wrote:
> int match_foo(const char *s)
> {
> static const struct regex *const regex = ®exes[0];
> // static regmatch_t *const matches = regex->matches;
> // static regmatch_t *const matches = regexes[0].matches;
> static regmatch_t *const matches = foo_matches;
>
> return regexec(®ex->regex, s, regex->nmatch, matches, 0);
> }
>
> Note the two commented-out initializers for "matches". I would much
> prefer to write the initializer in terms of the "regex" pointer or the
> array. This would make the connection much clearer and make any future
> changes to the code much less error-prone. Unfortunately, neither of
> the commented-out initializers works; gcc (rightly) complains that the
> initializer elements is not constant.
>
> I cannot make the array itself constant, because of the need to compile
> and free the "regex" members.
>
> As far as I know, my options are:
>
> 1. Live with the current scheme.
>
> 2. Change "matches" in the function to an automatic variable.
>
> 3. Change the "regex" member of "struct regex" to a pointer and
> create separate foo_regex, bar_regex, etc. objects. I could then
> make the "regexes" array constant.
>
> Am I missing anything?
4. Use C++ instead of C, which allows those initializers.
Probably not what you were looking for though.
More information about the Gcc-help
mailing list