c++linkerunreal-engine4undef

Can #undef affect member functions in C++?


I have an Unreal Engine 4 project with several plugins. On of these plugins contains a FileHelper class with a method CreateFile. This has worked fine for months, but in a recent commit, a different plugin added a call to FileHelper::CreateFile and now sometimes I get a linker error saying that CreateFileW is not a member of FileHelper (this doesn't appear in every build, which I cannot yet explain). I went on to undefine CreateFile temporarily like this

#include "UtilPlugin/File/FileSystemHelper.h"

#ifdef _WIN32
#pragma push_macro("CreateFile")
#undef CreateFile
#endif //_WIN32

...//new code including CreateFile call

#ifdef _WIN32
#pragma pop_macro("CreateFile")
#endif //_WIN32

but now I get errors

C2039 'CreateFile': is not a member of 'FileSystemHelper'

C3861 'CreateFile': identifier not found

Since I know CreateFile is successfully called in other places (at least within the same plugin as the FileSystemHelper), I know that it exists.

Thus my question is, if the undefine can affect member functions like this. I have moved the #undef part above the includes in code and I don't get an error anymore, but since it occurred seemingly randomly, I'm not entirely sure that I really fixed the problem.


Solution

  • Following shows a problematic case:

    #define CreateFile CreateFileW
    
    struct S
    {
        void CreateFile(); // Actually void CreateFileW();
    };
    

    And then

    #undef CreateFile
    
    void foo()
    {
       S s;
       s.CreateFile(); // Error, "expect" s.CreateFileW()
    }
    

    As #define might modify meaning of code (locally), #undef "cancels" that modification locally too.