14

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

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

dom0
  • 7,356
  • 3
  • 28
  • 50
Gad D Lord
  • 6,620
  • 12
  • 60
  • 106

6 Answers6

49

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

Q_DECL_DEPRECATED void foo();
Tim Sutton
  • 661
  • 5
  • 4
  • 1
    I my case, I do use the attribute after the function name void foo() Q_DECL_DEPRECATED; – Ratah Oct 12 '18 at 14:54
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).

Community
  • 1
  • 1
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
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.
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
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.

Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
  • 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. – Ken Bloom Nov 15 '10 at 05:11
  • @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.* – Yippie-Ki-Yay Nov 15 '10 at 09:04
  • 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. – Ken Bloom Nov 15 '10 at 14:50
0

2023:

Yes, the Qt is defined Q_DEPRECATED and Q_DEPRECATED_X macros for this purpose:

Q_DEPRECATED void myOldFunc();
Q_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+.

S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59
-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.

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168