Includes for forward declarations marked as unused

Hi.

It seems that ReSharper cannot detect that an include in a C++ is intended to provide the declaration for a forward declaration in the header file.

For example I have a field on the header file of a type that is forward declared. In the cpp file the type is not mentioned, but I need to include the header of the type of my field.

Is there any way make ReSharper not mark the includes as unused without suppressing the warnings?

Thanks.

Lutfi

3 comments
Comment actions Permalink

Hello Lutfi,

I'm sorry, I'm not sure I understand your example correctly. Could you please show example code where this problem occurs?

Thanks!

0
Comment actions Permalink

Hi Igor.


Let's say we have ClassA.h:

    class ClassB; // forward declaration

    class ClassA {
        ClassB myField;

        int method();

    }

and ClassA.cpp:

    #include "ClassA.h"
    #include "ClassB.h" --> This line is marked as possibly unused by ReSharper.

    int ClassA::method() {
        myField.value;
    }

 

0
Comment actions Permalink

Lutfi, thanks for the source code! Forward declaration of a type is not sufficient to declare a class member with this type, so if the code compiles, it means that the definition of ClassB was seen before ClassA.h was included. Сonsequently, the second include is indeed redundant in the source file.

0

Please sign in to leave a comment.