tags:

views:

2879

answers:

4

Hi.
I have a method in an interface that I want to deprecate with portable C++. When I Googled for this all I got was a Microsoft specific solution. (http://msdn.microsoft.com/en-us/library/c8xdzzhh(VS.80).aspx and http://msdn.microsoft.com/en-us/library/044swk7y(VS.80).aspx)

A second prize solution would be to ifdef a MSVC and a GCC solution.
Thanks

A: 

I don't think C++ has support for expressing that a method should not be used.

If your API is documented, that is the obvious way to mark it. In (the highly recommended, by the way) Doxygen markup, you use the "\deprecated" tag to do this.

unwind
+5  A: 

In GCC you can declare your function with the attribute deprecated like this:

void myfunc() __attribute__ ((deprecated));

This will trigger a compile-time warning when that function is used in a .c file.

You can find more info under "Diagnostic pragmas" at http://gcc.gnu.org/onlinedocs/gcc/Pragmas.html

Terje Mikal
+2  A: 

Dealing with portable projects it's almost inevitable that you at some point need a section of preprocessed alternatives for a range of platforms. #ifdef this #ifdef that and so on.

In such a section you could very well conditionally define a way to deprecate symbols. My preference is usually to define a "warning" macro since most toolchains support custom compiler warnings. Then you can go on with a specific warning macro for deprecation etc. For the platforms supporting dedicated deprecation methods you can use that instead of warnings.

sharkin
+15  A: 

This should do the trick:

#ifdef __GNUC__
#define DEPRECATED(func) func __attribute__ ((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED(func) __declspec(deprecated) func
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED(func) func
#endif

...

//don't use me any more
DEPRECATED(void OldFunc(int a, float b));

//use me instead
void NewFunc(int a, double b);

However, you will encounter problems if a function return type has a commas in its name e.g. std::pair<int, int> as this will be interpreted by the preprocesor as passing 2 arguments to the DEPRECATED macro. In that case you would have to typedef the return type.

Michael
Instead of #error, it would be better to #define DEPRECATED(func) func
CesarB
@CesarB: I disagree; if there were no error, there would be no way to notice that something unexpected happened.
mxp
mxp: The deprecation is only a warning, and hence I'd say that a warning that it isn't supported is all you need.
Leon Timmermans
Yep, I'd go for "#warning You need to implement DEPRECATED for this compiler", or some such. If that's impossible, then the porter can #define DEPRECATED(FUNC) FUNC, and live without it.
Steve Jessop
Agreed, edited accordingly
Michael
Unfortunately there's no standard way to output a compile warning in C++ :P#pragma message will have to do.
Michael
Is it possible to mark a class Deprecated using this macro ? with something like DEPRECATED(Class()); ?
dzen