Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GCC Format attribute not working on function pointer with "using" alias

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

Link to example: https://godbolt.org/z/qWdxWq6Gx

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading