As the ISO C++ Guidelines recommends, we should use using instead of typedef. However, recently I had to code some debug logging where the attribute would be helpful for compile-time diagnostics.
When I tried applying the using keyword, this did not seem to work:
using logger_cb = void(*)(const char*, va_list)
__attribute__ ((__format__ (__printf__, 1, 0)));
The error is:
<source>:4:48: error: expected ';' before '__attribute__'
4 | using logger_cb = void(*)(const char*, va_list) __attribute__ ((__format__ (__printf__, 1, 0))); // <-- uncommenting this won't work
| ^~~~~~~~~~~~~~
| ;
However, using typedef it works:
typedef void (*logger_cb)(const char*, va_list)
__attribute__ ((__format__ (__printf__, 1, 0)));
I cannot figure out if my syntax is wrong or this simply is not supported with the using keyword. Does some guru know about this?
When I tried applying the using keyword, this did not seem to work:
The placement of the optional attribute-specifier-seq must be after the identifier in an alias-declaration. This can be seen from alias declaration:
Alias declarations are declarations with the following syntax:
using identifier attr (optional) = type-id ; (1) template < template-parameter-list > using identifier attr (optional) = type-id ; (2)attr - optional sequence of any number of attributes
The same can be seen from gram.dcl:
alias-declaration: using identifier attribute-specifier-seqopt = defining-type-id ;
This means that the standard way to use using here is as shown below:
using logger_cb [[gnu::format (__printf__, 1, 0) ]] = void(*)(const char*, va_list);
Working demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With