[Qt-interest] Advices on safer SIGNAL macro

Johannes Schaub schaub.johannes at googlemail.com
Thu Oct 20 21:24:14 CEST 2011


Mandeep Sandhu wrote:

> On Thu, Oct 20, 2011 at 1:17 AM, Johannes Schaub
> <schaub.johannes at googlemail.com> wrote:
>> I've written myself a custom SIGNAL macro that at compile time checks
>> that all parameter types I gave in the signature passed to SIGNAL are
>> known at the point of use of SIGNAL:
>>
>> namespace static_signal_detail {
>> template<typename>
>> struct take { };
>> }
>>
>> #define STATIC_SIGNAL(SIG) (static_signal_detail::take<void(void SIG)>(),
>> #\
>> SIGNAL(SIG))
>>
> 
> Interesting. How does this work?
> 

Nothing too fancy. Taking "SIG" as "changed(int)" then "void(void 
changed(int))" is a function type which is actually equivalent to 
"void(void(*)())" meaning that the parameter name "changed" is superfluous 
and that the nested function type "void()" is transformed to be a function 
pointer type "void(*)()", just like arrays are transformed to be pointers to 
their element type when they are used as declared type of a parameter. This 
type is passed as an argument to the "take" class template. 

The comma operator is used to yield the value of  "SIGNAL(changed(int))" and 
ignore the first part, only used to validate that the parameter types are 
real existing types.





More information about the Qt-interest-old mailing list