Avatar of osnat
osnat

asked on 

How to insert a compile time warning to a macro

I have an old macro that I wish to delete. I want all the other programmer not to use it any more. So I want to insert a warning that will be displayed at compile time, when this macro is used.
How can I do it?
CC++

Avatar of undefined
Last Comment
evilrix
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

If you have access to Boost you can take advantage of the BOOST_STATIC_WARNING macro.
#include <boost/static_warning.hpp>
 
#define DEPRECATED_MACRO \
	BOOST_STATIC_WARNING(false); \
	int i=0; // DO something deprecated
 
int main()
{
	DEPRECATED_MACRO // Comment this out and the warning should disappear.
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

Something like this :
#define MACRO _Pragma("\"Warning: deprecated macro 'MACRO' called\"") something

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> Something like this
_Pragma won't work on all compilers.
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> >> Something like this
>> _Pragma won't work on all compilers.

It should on C99 compliant compilers.

I posted it, because if osnat's compiler supports it (and most recent compilers do), it's imo cleaner and simpler.
Avatar of osnat
osnat

ASKER

This is for evilrix:
I used your solution, but when I use the macro, I get the following ERROR in compile time:

error: expected a declaration
        USE_SLOTS (SegmentPencilImp);

Where USE_SLOTS is the deprecated macro
Avatar of Infinity08
Infinity08
Flag of Belgium image

You don't have to accept an answer yet if you still have problems/doubts.

Also take a look at the other suggestions posted ... Maybe one of those suits you better.
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> I used your solution, but when I use the macro, I get the following ERROR in compile time:
Can you post the code please?
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

^^^It's probably just a simple syntax error we can sort out quite easily :)
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo