GCC 15.2中带printf format属性的模板构造函数参数计数报错问题咨询
GCC 15.2中带printf format属性的模板构造函数参数计数报错问题咨询
我之前在Stack Overflow上看到过一个类似的问题帖,但那个帖子的解决方案说这是个已经修复的GCC bug。现在我用GCC 15.2编译代码时又碰到了类似的错误,难道这个bug又重现了吗?
我写了下面这段代码,编译时收到了三个覆盖所有重载的错误信息:
- error: ‘format’ attribute argument 3 value ‘7’ does not refer to a variable argument list
- error: ‘format’ attribute argument 3 value ‘6’ does not refer to a variable argument list
- error: ‘format’ attribute argument 3 value ‘4’ does not refer to a variable argument list
我查阅了GCC 15.2的官方文档,完全找不到报错的原因。按照我的理解,构造函数的第一个隐式参数是this,所以参数计数应该从2开始,formatString是第4个显式参数,对应属性里的位置5,可变参数对应位置6,这和我写的__attribute__ ((format(printf, 5, 6)))是一致的啊。
我到底哪里弄错了?是不是漏看了GCC文档里关于模板函数使用__attribute__的特殊规则?
#include <vector> #include <source_location> #include <stdio.h> typedef unsigned int uint; constexpr std::size_t MAX_LOG_LINE_LENGTH = { 2'500 }; enum LogControllerMode { Default = 0, }; enum class LogControllerType { Normal, }; template <typename... Ts> struct FAEWE_LOG { __attribute__ ((format(printf, 5, 6))) FAEWE_LOG( const uint instrumentsId, [[maybe_unused]] const LogControllerMode logMode, [[maybe_unused]] const std::vector<LogControllerType> & logControllerType, const char * formatString, Ts &&... LogArguments, const std::source_location & sourceLocation = std::source_location::current() ) { char resolvedParameters [MAX_LOG_LINE_LENGTH]; snprintf( resolvedParameters, sizeof(resolvedParameters), formatString, LogArguments...); printf( "%s:%d - [%d] %s\n", sourceLocation.file_name(), sourceLocation.line(), instrumentsId, resolvedParameters ); } }; template <typename... Ts> FAEWE_LOG( const uint, const LogControllerMode, const std::vector<LogControllerType>, const char *, Ts &&... ) -> FAEWE_LOG<Ts...>; auto main () -> int { FAEWE_LOG( 5, LogControllerMode::Default, { LogControllerType::Normal }, " This is not a correct printf %s string %d", "formatted", 73U ); return 0; }
更新:对了,我忘了说,这段代码的报错情况可以在在线编译器里复现。
备注:内容来源于stack exchange,提问作者Peter VARGA
相关产品推荐
相关产品推荐

