0

The following compiles and runs fine on Windows MSVC compiler (Visual Studio IDE 2019):

#include <iostream>
#include <boost/stacktrace.hpp>

//class INPUT {
//    int xyz;
//};

void function() {
    std::cout << boost::stacktrace::stacktrace();
}

int main() {
    function();
}

and produces the correct stacktrace inside of function().

However, on uncommenting out my class definition called INPUT, it fails to compile because of a name clash with a similarly named class in internal Windows file, WinUser.h

typedef struct tagINPUT {
    DWORD   type;

    union
    {
        MOUSEINPUT      mi;
        KEYBDINPUT      ki;
        HARDWAREINPUT   hi;
    } DUMMYUNIONNAME;
} INPUT, *PINPUT, FAR* LPINPUT;

Apart from renaming my class differently, is there any other way to avoid such name clashes?

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • 1
    Hide the boost header in a different translation unit? – HolyBlackCat Aug 26 '23 at 12:47
  • 3
    Put your declarations/definitions in a namespace unique to your project. Just have `main` as the only top level name. – Richard Critten Aug 26 '23 at 12:47
  • @RichardCritten Indeed. I was able to get the code to run enclosing the definition of INPUT within my own userdefined namespace. Thx! – Tryer Aug 26 '23 at 13:23
  • @RichardCritten Would you suggest that functions/global variables that are externed in other translation units should also remain within user defined namespaces? I.e., taking your comment literally, should `int main(){...}` be the **only** user defined function/thing in the global top level? Then, should other translation units have a `using namespace ;` to refer to these externed functions/variables? – Tryer Aug 26 '23 at 15:59
  • 1
    I always put my programs, libraries etc into their own unique namespaces; unless there is an ABI requirement for a top-level name eg `main` or to interface with `C` where I have no idea how `namespace` and `extern "C"` would interact. I make heavy use of name aliases to shorten namespace names and hardly ever do `using namespace ....;` – Richard Critten Aug 26 '23 at 16:40
  • @RichardCritten boost stacktrace is highly problematic. On including the appropriate header file and defining a `max` function within my unique namespace, it complains because `max` is `#defined` in `minwindef.h` . Infact, it does not allow even `std::max` to work. Do you have any experience with this or how to deal with cases where the name you are using within your namespace is #defined elsewhere? Sorry for too many questions. I will open another one if this exchange is turning out to be more requirement of your time. – Tryer Aug 26 '23 at 17:28
  • @RichardCritten Please don't bother. This is apparently a known issue and the fix was discussed over at https://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max Thanks for your time. – Tryer Aug 26 '23 at 17:39

0 Answers0