https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116954
Bug ID: 116954 Summary: -Wsuggest-attribute=format false positive for function template Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: m.gcc.gnu.org at alias dot viem.se Target Milestone: --- When defining a function template that calls a standard printf function there will a -Wsuggest-attribute=format warning when instantiated. One can not att attribute to function definition template. And adding a function declaration template with attribute does not help. /* Demonstrate -Wsuggest-attribute=format false positive for function template. Verified also for x86-64.gcc 13.1, 13.2, 13.3, 14.1, 14.2 on godbolt.org (2024-10-03). > g++ --version g++ (Gentoo 13.3.1_p20240614 p17) 13.3.1 20240614 ... > g++ -Wmissing-format-attribute temp-format.cpp <source>: In instantiation of 'int fn(char (&)[N], const char*, ...) [with long unsigned int N = 20]': <source>:21:12: required from here <source>:13:27: warning: function 'int fn(char (&)[N], const char*, ...) [with long unsigned int N = 20]' might be a candidate for 'gnu_printf' format attribute [-Wsuggest-attribute=format] 13 | int ret = std::vsnprintf(buf, N, fmt, ap); | ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ */ #include<cstdarg> #include<cstdio> template<std::size_t N> int fn(char (&buf)[N], const char fmt[], ...) __attribute__ ((__format__ (__printf__, 2, 3))); template<std::size_t N> int fn(char (&buf)[N], const char fmt[], ...) // __attribute__ ((__format__ (__printf__, 2, 3))) // error: attributes are not allowed on a function-definition { va_list ap; va_start(ap, fmt); int ret = std::vsnprintf(buf, N, fmt, ap); // warning: function âint fn(char (&)[N], const char*, ...) [with long unsigned int N = 20]â might be a candidate for âgnu_printfâ format attribute [-Wsuggest-attribute=format] va_end(ap); return ret; } int main() { char buf[20]; return fn(buf, "%d", 42); }