如何将 Qt 中的某些内容标记为过时(已弃用)?
Qt 4.7 中的 C++ 中是否存在 Q_OBSOLETE 或 Q_DEPRECATED?
或者有类似的C++宏或关键字吗?
Is there Q_OBSOLETE or Q_DEPRECATED in C++ with Qt 4.7?
Or is there a similar C++ macro or keyword?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尽管不是 C++ 标准,但只要使用该
指令
就不太可能遇到不支持它的编译器 (请参阅这个问题)。
Just use the
directive
although is not C++ standard is quite unlikely you will encounter a compiler that does not support it (see this SO question).
2023:
是的,Qt 为此定义了
QT_DEPRECATED
和QT_DEPRECATED_X
宏:区别只是文本消息。
此外,C++14 将
[[deprecated("text")]]
属性作为标准提供。如果您使用 C++ 14+,则似乎会在幕后使用此属性。2023:
Yes, the Qt is defined
QT_DEPRECATED
andQT_DEPRECATED_X
macros for this purpose:difference is just the text message.
Also the C++14 presents
[[deprecated("text")]]
attribute as standard. It seems that this attribute is used under the hood if you use C++ 14+.您可能想要自己做一些类似的事情:
如果没有定义 Q_TREAT_OBSOLETE_AS_ERRORS ,此构造只是替换一些已弃用的代码/部分代码,否则会生成编译时错误。
请注意,
BOOST_STATIC_ASSERT
没有范围限制,Q_OBSOLETE
宏也是如此。这可能不是解决您的问题的最佳方法,实际上我不确定这是否有用。
您可以将代码标记为
@obsolete
或简单地在注释中指出。You might want to do something similiar yourself:
This construction simply substitutes some deprecated code / part of code if there is no
Q_TREAT_OBSOLETE_AS_ERRORS
defined and generates compilation-time error otherwise.Note that
BOOST_STATIC_ASSERT
has no scope limitations, so does theQ_OBSOLETE
macro.Probably this is not the best way to solve your problem and actually I'm not sure this is useful.
You might just mark the code as
@obsolete
or simply point it out in the comments.通过“不推荐使用的构造”,您真正的意思是“不推荐使用的成员函数”。您要求编译时警告以引起您对任何已弃用函数的调用站点的注意。
在标准 C++ 中这是不可能以任何合理的方式实现的,而且我在 G++ 中也没有看到任何支持这一点的属性。如果编译器还没有对 Qt 的支持,那么 Qt 就无法真正添加这样的功能。
但是,Microsoft Visual C++ 支持
__declspec(已弃用)
扩展,我想可以为 G++ 4.5 编写一个添加类似功能的编译器插件。By "deprecated constructs", you really mean "deprecated member functions". You're asking for a compile-time warning to draw your attention to the call site of any deprecated function.
This isn't possible in any reasonable way in standard C++, and I don't see any attributes in G++ that would support this either. Qt can't really add a feature like that if the compiler doesn't have some support for it already.
However, Microsoft Visual C++ supports an
__declspec(deprecated)
extension, and I would imagine it's possible to write a compiler plugin for G++ 4.5 that adds a similar feature.如果您使用 Q_DECL_DEPRECATED 您应该得到您正在寻找的结果,例如:
If you use Q_DECL_DEPRECATED you should get the outcome you are looking for e.g.: