Dženan
Dženan

Reputation: 3395

Is there a cross-platform way to disable deprecated warnings in C++?

I have a library, and I am refactoring some functionality. I marked some old methods with itkLegacyMacro defined below. These deprecated methods are being called from the library's own unit tests. Is there a way to disable deprecation warnings which will work across all (or at least most) compilers?

itkLegacyMacro:

// Setup compile-time warnings for uses of deprecated methods if
// possible on this compiler.
#if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
#define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
#elif defined( _MSC_VER )
#define itkLegacyMacro(method) __declspec(deprecated) method
#else
#define itkLegacyMacro(method) method
#endif

Method definition in library proper:

class X {
itkLegacyMacro(void oldMethod());
void newMethod(); }

Method invocation from unit test:

X testX;
testX.newMethod(); //test the new stuff
testX.oldMethod(); //test the old stuff too!

The last line causes a warning to be emitted when compiled. I would like this library to test the deprecated functionality, but not have warnings when compiled. Is that possible? C++11 is being used.

Upvotes: 2

Views: 1014

Answers (1)

Jens
Jens

Reputation: 9406

As far as I know __declspec(...) is a Microsoft extension and is not cross-plattform anyway.

You could use macros to control this

#ifdef _MSC_VER
#define DEPRECATED __declspec(deprecated)
#else 
#define DEPRECATED
#endif

DEPRECATED void someDeprecatedFunction()

There is also [[deprecated("because")]] since C++14.

To turn it off only for unit tests, you could do something like

#ifndef SUPPRESS_DEPRECATE_FUNCTIONS
#define DEPRECATED __declspec(deprecated)
#else
#define DEPRECATED
#endif

and then #define SUPPRESS_DEPRECATE_FUNCTIONS in your unit test, or compile with -DSUPPRESS_DEPRECATE_FUNCTIONS. Or you could create a special header in your unit tests which #pragma the warning suppression. Something along the lines of

#if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
#pragma for gcc
#elif defined( _MSC_VER )
#pragma for msvc
#else
// nothing
#endif

#include "your_library_header.h"

The unit tests then only include this header before any other of your library headers.

Upvotes: 1

Related Questions