Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C, Portable way to deprecate struct members

Tags:

c

struct

With GCC & Clang you can deprecate struct members, (as shown below).

However I didn't see a way to do this for other compilers (MSVC for example).

While this isn't apart of the C spec and most likely relies on pragma's or custom extensions for each compiler, it would be useful to be able to support this across a wider range of compilers.

/* mytest.h */
#ifdef __GNUC__
#  define ATTR_DEPRECATED __attribute__((deprecated))
#else
#  define ATTR_DEPRECATED  /* unsupported compiler */
#endif

struct Test {
  int bar;
  int foo  ATTR_DEPRECATED;
};

Once a member is deprecated, the compiler should warn of its use if its accessed directly, eg:

#include "mytest.h"
static func(void)
{
    Test t;
    t.bar = 1;
    t.foo = 0;  /* <-- WARN ABOUT THIS AT COMPILE TIME */
}
like image 992
ideasman42 Avatar asked Oct 21 '22 11:10

ideasman42


1 Answers

Besides compilers which support GCC's __attribute__((deprecated)) (Clang for example) there aren't conventions for MSVC to deprecate struct members (With MSVC you can deprecate the identifier but that applies globally, not just to that struct).

Also no other conventions for other C compilers have been posted. So it looks like this is GCC specific.

like image 126
ideasman42 Avatar answered Nov 12 '22 22:11

ideasman42