This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Sanity check - making a static initializer constant
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Ian Pilcher <arequipeno at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Thu, 10 Oct 2013 14:50:08 +0100
- Subject: Re: Sanity check - making a static initializer constant
- Authentication-results: sourceware.org; auth=none
- References: <5255AA2F dot 1020404 at gmail dot com>
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.