Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate deprecated warning for a method in a COM interface (c++)

We are using a "COM-like" model where I work, as we generally follow COM rules, but do not perform MIDL compiling and do not target other languages than c/c++. As such, I know I can always bend the rules to suit my needs, but I try not to since we might want to become truly COM-compliant someday and if that day comes, we want it to be as painless as possible.

I want to print out a warning when anyone compiles code using a certain method from a specific interface. In c++ (we only support Microsoft compiler), we would add __declspec(deprecated) before the function declaration to achieve our goal.

Can I just add this in front of my interface method declaration or is there a better, more COM way to do that?

I would also like to know if only adding __declspec(deprecated) in front of the method is enough to force users to recompile (I would like to avoid that, if possible).

Thanks

Update

I tried using __declspec(deprecated) in front of my method declaration like this:

struct Interface : public IUnknown
{
    __declspec(deprecated) virtual HRESULT __stdcall OldMethod
    (
        int Arg1;
        int Arg2;
    ) = 0;

    virtual HRESULT __stdcall NewMethod
    (
        //arguments...
    ) = 0;
}

With this way of deprecating a method, I get no compiler warning at all when trying to use OldMethod. Is there a limitation that I don't know about using __declspec(deprecated) with pure virtual methods?

Thanks again

like image 767
franmon Avatar asked May 25 '12 23:05

franmon


People also ask

How do you mark a method as deprecated?

To mark a method as deprecated we can use the JavaDoc @deprecated tag. This is what we did since the beginning of Java. But when a new metadata support introduced to the Java language we can also use annotation. The annotation for marking method as deprecated is @Depreated .

How do you mark a method as deprecated in C++?

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

What is deprecated in C?

Deprecated means the use of the name or entity declared with this attribute is allowed but discouraged for some reason. The compiler gives warnings and if string literals are provided, they are included in warnings.

Can we use deprecated methods in C#?

Never use deprecated fields, methods, or classes in new code.


1 Answers

__declspec(deprecated) is a source-level feature of the MS C/C++ compiler, and won't affect the binary layout (ABI) of anything - so users will not have to recompile. The effect is to issue a compile-time warning to anyone calling that method via including your C++ header file. It will have no effect on people using your object as a COM object without including your header, e.g. from another language. I don't believe COM/IDL provides an analogous 'deprecated' attribute itself.

So in summary, you may as well add the attribute for the benefit of the C++ users, but if you ever provide for general COM consumers, they will have to rely on documentation you provide to know that the method is deprecated.

like image 57
ndkrempel Avatar answered Sep 27 '22 18:09

ndkrempel