C++ Mark as Deprecated

C++ mark as deprecated

In C++14, you can mark a function as deprecated using the [[deprecated]] attribute (see section 7.6.5 [dcl.attr.deprecated]).

The attribute-token deprecated can be used to mark names and entities whose use is still allowed, but is discouraged for some reason.

For example, the following function foo is deprecated:

[[deprecated]]
void foo(int);

It is possible to provide a message that describes why the name or entity was deprecated:

[[deprecated("Replaced by bar, which has an improved interface")]]
void foo(int);

The message must be a string literal.

For further details, see “Marking as deprecated in C++14”.

Mark as deprecated function parameters in C++14

The rule is that the attribute is valid on, amongst other things, variable declarations (broadly). It's not specifically permitted for such declarations found in function arguments.

The original proposal, N3394, doesn't mention such a use case, either, and neither does the documentation for the original feature in GCC (which regardless accepts the equivalent usage) or in VS (I didn't check Clang).

As such, I think it's an "accident" that this is permitted, not something that anyone really had in mind as being useful.

Could it be useful to document deprecated defaulted arguments, as Artyer explores? Yes, potentially, and vaguely. But as Artyer also found, mainstream compilers don't actually react to this usage in a helpful manner.

So, at the present time, it's not useful, and the language feature wasn't particularly designed to be useful in this case.

Mark class/method obsolete or deprecated in C++

The easiest way is with a #define DEPRECATED. On GCC, it expands to __attribute__((deprecated)), on Visual C++ it expands to __declspec(deprecated), and on compilers that do not have something silimar it expands to nothing.

Marking functions from a library as deprecated

Pulled from a working project

#ifdef __GNUC__
#define DEPRECATED(X) X __attribute__((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED(X) __declspec(deprecated) X
#else
#define DEPRECATED(X) X
#endif

See http://msdn.microsoft.com/en-us/library/dabb5z75.aspx and http://msdn.microsoft.com/en-us/library/044swk7y.aspx

Then

DEPRECATED(void foo(int a, int b, int c));

Mark property as deprecated in Objective C

Unless you really want to deprecate based on iOS version, which I suspect you don't want to do, you can use DEPRECATED_ATTRIBUTE

@property (strong) NSObject *object DEPRECATED_ATTRIBUTE;

How to mark a method as obsolete or deprecated?

The shortest way is by adding the ObsoleteAttribute as an attribute to the method. Make sure to include an appropriate explanation:

[Obsolete("Method1 is deprecated, please use Method2 instead.")]
public void Method1()
{ … }

You can also cause the compilation to fail, treating the usage of the method as an error instead of warning, if the method is called from somewhere in code like this:

[Obsolete("Method1 is deprecated, please use Method2 instead.", true)]

C programming: How to deprecate INLINE function

static inline __attribute__((deprecated)) void Foo()
{

}

works for me. It generates a warning when I try to use it.

However I am not sure what the __STATIC_INLINE is?

EDIT according to your comment:

Do you mean something like

#define THIS_IS_DEPRECATED __attribute__((deprecated))

static inline THIS_IS_DEPRECATED void Foo()
{

}

How do I flag a method as deprecated in Objective-C 2.0?

Deprecation Syntax

Syntax is provided to mark methods as deprecated:

@interface SomeClass
-method __attribute__((deprecated));
@end

or:

#include <AvailabilityMacros.h>
@interface SomeClass
-method DEPRECATED_ATTRIBUTE; // or some other deployment-target-specific macro
@end


Related Topics



Leave a reply



Submit