g++6.3中[[deprecated]]对typedef生效、对using声明失效是否为预期?
在GCC 6.3中,[[deprecated]]对typedef有效但对using别名无效——是预期行为还是bug?
这绝对是GCC 6.3版本的bug,而非预期设计。
原因分析
C11及后续标准明确允许将[[deprecated]]这类属性应用于所有类型别名声明,不管是typedef还是using形式的别名。问题出在GCC 6.x系列对C属性语法的解析支持不完善:
- 对于
typedef声明,编译器能正确识别属性是对该类型别名的修饰; - 但对于
using形式的类型别名,旧版本GCC会错误地将属性后的using判定为无效语法,直接抛出编译错误,也就是你遇到的expected unqualified-id before ‘using’提示。
验证与解决方案
这个兼容性问题在GCC 7及更高版本中已经被完全修复。如果你升级到GCC 7+,启用[[deprecated]]标记的using别名代码:
[[deprecated("use the namespace 'our'")]] using myvector = std::vector<float>;
会和typedef版本表现完全一致:正常编译,并输出预期的废弃警告。
你的最小复现示例验证
在GCC 6.3中,typedef版本能正常触发警告,而using版本报错;但在GCC 7+环境下,两者都会输出类似如下的警告信息:
main.cpp: In function ‘int main()’: main.cpp:11:12: warning: ‘myvector’ is deprecated: use the namespace 'our' [-Wdeprecated-declarations] myvector a; ^ main.cpp:7:70: note: declared here [[deprecated("use the namespace 'our'")]] using myvector = std::vector<float>; ^~~~~~~~
内容的提问来源于stack exchange,提问作者pseyfert
相关产品推荐
相关产品推荐

