如何将 Qt 中的某些内容标记为过时(已弃用)?

发布于 2024-10-02 14:11:52 字数 78 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风情万种。 2024-10-09 14:11:53

尽管不是 C++ 标准,但只要使用该

#warning 

指令

就不太可能遇到不支持它的编译器 (请参阅这个问题)。

Just use the

#warning 

directive

although is not C++ standard is quite unlikely you will encounter a compiler that does not support it (see this SO question).

请远离我 2024-10-09 14:11:53

2023:

是的,Qt 为此定义了 QT_DEPRECATEDQT_DEPRECATED_X 宏:

QT_DEPRECATED void myOldFunc();
QT_DEPRECATED_X("use myNewFunc instead") void myOldFunc2();

区别只是文本消息。
此外,C++14 将 [[deprecated("text")]] 属性作为标准提供。如果您使用 C++ 14+,则似乎会在幕后使用此属性。

2023:

Yes, the Qt is defined QT_DEPRECATED and QT_DEPRECATED_X macros for this purpose:

QT_DEPRECATED void myOldFunc();
QT_DEPRECATED_X("use myNewFunc instead") void myOldFunc2();

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+.

探春 2024-10-09 14:11:53

您可能想要自己做一些类似的事情:

#ifdef Q_TREAT_OBSOLETE_AS_ERRORS
#define Q_OBSOLETE(X) \
        BOOST_STATIC_ASSERT(false); \
        X

#else 
#define Q_OBSOLETE(X) X
#endif

如果没有定义 Q_TREAT_OBSOLETE_AS_ERRORS ,此构造只是替换一些已弃用的代码/部分代码,否则会生成编译时错误。

请注意,BOOST_STATIC_ASSERT 没有范围限制,Q_OBSOLETE 宏也是如此。

这可能不是解决您的问题的最佳方法,实际上我不确定这是否有用

您可以将代码标记为 @obsolete 或简单地在注释中指出。

You might want to do something similiar yourself:

#ifdef Q_TREAT_OBSOLETE_AS_ERRORS
#define Q_OBSOLETE(X) \
        BOOST_STATIC_ASSERT(false); \
        X

#else 
#define Q_OBSOLETE(X) X
#endif

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 the Q_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.

逆光飞翔i 2024-10-09 14:11:53

通过“不推荐使用的构造”,您真正的意思是“不推荐使用的成员函数”。您要求编译时警告以引起您对任何已弃用函数的调用站点的注意。

在标准 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.

一百个冬季 2024-10-09 14:11:52

如果您使用 Q_DECL_DEPRECATED 您应该得到您正在寻找的结果,例如:

Q_DECL_DEPRECATED void foo();

If you use Q_DECL_DEPRECATED you should get the outcome you are looking for e.g.:

Q_DECL_DEPRECATED void foo();
菊凝晚露 2024-10-09 14:11:52
  1. 将真正的功能从公共范围中拉出来。
  2. 在公共范围内创建另一个同名的函数。
  3. 在该函数中插入警告/失败代码。
  4. 用新来称呼原来的。
  1. Pull the real function out of public scope.
  2. Create another function with the same name in public scope.
  3. Insert your warning/fail code in that function.
  4. Call the original with the new.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文