franmon
franmon

Reputation: 2227

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

Upvotes: 5

Views: 2702

Answers (1)

ndkrempel
ndkrempel

Reputation: 1936

__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.

Upvotes: 6

Related Questions