https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89299
Bug ID: 89299 Summary: __attribute__ cleanup does not accept template function Product: gcc Version: 8.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: paradox_ptr at protonmail dot ch Target Milestone: --- #include <cstdlib> template <typename T> void cu( T** p ) { if ( p && *p ) { free( *p ); } } void cu_char( char** s ) { if ( s && *s ) { free ( *s ); } } int main() { __attribute__((cleanup(cu))) char* s = nullptr; return 0; } Using template function for cleanup yields error: $ g++ main.cpp --std=c++14 -Wall -Wextra main.cpp: In function ‘int main()’: main.cpp:13:37: error: cleanup argument not a function __attribute__((cleanup(cu))) char* s = nullptr; ^ main.cpp:13:37: warning: unused variable ‘s’ [-Wunused-variable] Same for cleanup(cu<char>). cleanup(cu_char) works, as expected. Clearly, template function is still a function, especially when explicitly instantiated. GCC documentation for Common Variable Attributes does not state any limitations for this function. $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-8.2.0-r6/work/gcc-8.2.0/configure --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/8.2.0 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/8.2.0 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/8.2.0/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/8.2.0/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include/g++-v8 --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/8.2.0/python --enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt --disable-werror --with-system-zlib --enable-nls --without-included-gettext --enable-checking=release --with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 8.2.0-r6 p1.7' --disable-esp --enable-libstdcxx-time --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-multilib --with-multilib-list=m32,m64 --disable-altivec --disable-fixed-point --enable-targets=all --enable-libgomp --disable-libmudflap --disable-libssp --disable-libmpx --disable-systemtap --enable-vtable-verify --enable-libvtv --enable-lto --without-isl --enable-default-pie --enable-default-ssp Thread model: posix gcc version 8.2.0 (Gentoo 8.2.0-r6 p1.7)