tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Some changes to autoconfiguration APIs



>> Here are two alternative approaches [...]

>> 1. C99 initializers:
>> 
>> 	config_found(self, whatever, (const struct cfargs *){
>> 	    .search = ...,
>> 	    .locators = ...,
>> 	})

That's not how C99 compound literals work.  That creates an anonymous
const struct cfargs * (ie, a pointer object) but then tries to
initialize it as if it instead were the struct it points to.  What you
probably want is more like

	config_found(self,whatever,&(const struct cfargs){
		...
	});

but you have to be aware that the anonymous const struct cfargs "has
automatic storage duration associated with the enclosing block"; it
goes away once the block exits.  Provided nobody saves pointers to the
struct cfargs or any of its components, this is fine, but I haven't
read the relevant code, so I don't know whether that's so.

There *is* an example (6.5.2.5 [#11]) that applies & to a compound
literal of struct type

       [#11]  EXAMPLE 3 Initializers  with  designations   can   be
       combined  with compound literals.  Structure objects created
       using compound literals can be passed to  functions  without
       depending on member order:

               drawline((struct point){.x=1, .y=1},
                       (struct point){.x=3, .y=4});

       Or, if drawline instead expected pointers to struct point:

               drawline(&(struct point){.x=1, .y=1},
                       &(struct point){.x=3, .y=4});

so I'm reasonably sure that has to work in C99.

If you want it to have static storage duration, I know of no way to do
that in C short of declaring a variable.  It can be done in gcc with a
statement expression

	config_found(..., ({ static const struct cfargs foo = { ... }; &foo; }));

but I don't know how widely supported that particular extension is, and
I seem to recall seeing it said in some form that depending on gcc-only
features was frowned upon.  However, I see little reason to do the
statement expression rather than

	{ static const struct cfargs foo = { ... };
	  config_found(..., &foo);
	}

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index