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?
The placement of the optional
attribute-specifier-seqmust be after the identifier in an alias-declaration. This can be seen from alias declaration:The same can be seen from gram.dcl:
This means that the standard way to use
usinghere is as shown below:Working demo