views:

238

answers:

2

Does the Sun compiler have a notation to mark functions as deprecated, like GCC's __attribute__ ((deprecated)) or MSVC's __declspec(deprecated)?

+1  A: 

This will get you a compiler warning on sun with the "+w" flag or on gcc with the "-Wall" flag. Unfortunately it breaks the ABI compatibility of the function; I haven't discovered a way around that yet.

#define DEPRECATED char=function_is_deprecated()

inline char function_is_deprecated()
{
    return 65535;
}

void foo(int x, DEPRECATED)
{
}

int main()
{
    foo(3);
    return 0;
}

The output:

CC -o test test.cpp +w
"test.cpp", line 7: Warning: Conversion of "int" value to "char" causes truncation.
"test.cpp", line 15:     Where: While instantiating "function_is_deprecated()".
"test.cpp", line 15:     Where: Instantiated from non-template code.
1 Warning(s) detected.

The way you use it is when you want to declare a function deprecated, you add a comma to the end of its parameter list and write DEPRECATED. The way it works under the hood is it adds a default argument (thus preserving API) that causes the conversion warning.

Joseph Garvin
Seems that the template argument <LINE> went missing somewhere.
MSalters
I was thinking wrong about when the template argument would get evaluated so I got rid of it.
Joseph Garvin
+1  A: 

It seems that one solution that would work on any compiler that supports #warning would be:

  • Copy the header in question to a new, promoted header name
  • Remove the deprecated functions from the promoted header file
  • Add to the old header file: #warning "This header is deprecated. Please use {new header name}"
Shmoopty