How to mark something in Qt as obsolete(deprecated)?
Asked Answered
T

6

14

Is there Q_OBSOLETE or Q_DEPRECATED in C++ with Qt 4.7?

Or is there a similar C++ macro or keyword?

Teevens answered 14/11, 2010 at 20:56 Comment(2)
What do you want such a macro or keyword to do?Wet
To issue a warning if deprecated constructs are found in use. See en.wikipedia.org/wiki/Deprecation for better explanation.Teevens
F
0

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

Facture answered 2/9, 2023 at 5:59 Comment(2)
But what's the difference between Q_DECL_DEPRECATED (suggested by the top answer, dating back to 2012) and Q_DEPRECATED? By the way, are you sure it's Q_DEPRECATED? In the docs I found the macros QT_DEPRECATED and QT_DEPRECATED_X("message") (notice: QT, not Q).Pereyra
@FabiosaysReinstateMonica, take a look at the qglobal.h for differences between that two macros. Some other macros rely on the QT_DEPRECATED. Also, yeah! You right. I had a misspelling about the Q_DEPRECATED.Facture
A
50

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

Q_DECL_DEPRECATED void foo();
Albany answered 3/4, 2012 at 21:53 Comment(1)
I my case, I do use the attribute after the function name void foo() Q_DECL_DEPRECATED;Peebles
A
1

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

Alp answered 15/11, 2010 at 4:24 Comment(1)
Windows / MSVC is a fairly common target with Qt, so I have to disagree with "quite unlikely."Franchot
D
1
  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.
Demonolatry answered 15/11, 2010 at 17:23 Comment(1)
Can't emit a compile time warning that way.Tails
D
0

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.

Dissect answered 14/11, 2010 at 21:49 Comment(3)
This doesn't work. This will cause compile errors if the deprecated code is compiled, whether or not that code is called from somewhere else.Tails
@Ken Well, it's obviously that you don't have a way to check if some code chunk is actually being called. This static assertions thing simply helps drawing attention to the compilation of deprecated features. Actually, all this Q_OBSOLETE stuff seems pretty unnecessary for me.Dissect
he's asking (I think) because he's used to Java, which can annotate functions as deprecated, and give you a compiler warning when you try to use them.Tails
F
0

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

Facture answered 2/9, 2023 at 5:59 Comment(2)
But what's the difference between Q_DECL_DEPRECATED (suggested by the top answer, dating back to 2012) and Q_DEPRECATED? By the way, are you sure it's Q_DEPRECATED? In the docs I found the macros QT_DEPRECATED and QT_DEPRECATED_X("message") (notice: QT, not Q).Pereyra
@FabiosaysReinstateMonica, take a look at the qglobal.h for differences between that two macros. Some other macros rely on the QT_DEPRECATED. Also, yeah! You right. I had a misspelling about the Q_DEPRECATED.Facture
T
-4

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.

Tails answered 15/11, 2010 at 5:10 Comment(2)
It seems I missed __attribute__((deprecated)) in G++.Tails
By the way in c++14 and so there's the [[deprecated]] keyword that you can add to some functions en.cppreference.com/w/cpp/language/attributesVallievalliere

© 2022 - 2024 — McMap. All rights reserved.