41

I ALWAYS THOUGHT YOU CAN DO THIS BUT NEVER TRIED IT

THIS IS MAGIC

Comments
  • 3
    Can you refer to functions through ncurses namespace? C noob here
  • 25
    @beegC0de Rather importantly, this is c++.

    ncurses is a c library though. Which means it is not in a namespace, as c doesn't support namespaces. So all of the functions are unprefixed. There are some functions which are rather common names, such as refresh();

    What I did was #include <ncurses.h> INSIDE of a namespace called ncurses. The way the preprocessor works is it literally just grabs the code from the ncurses.h file and puts it between those two brackets - think of it as copy/ paste every time it compiles. This means I can apply a namespace to a library which isn't normally namespaced! Beautiful.
  • 6
    I found this both helpful, and interesting. Thanks!
  • 3
    wait... how can this work? the ncurses.h probably has a lib too right? how will the compiler be able to resolve symbols from the lib now that the header contents r in a namespace? 🤔😯
  • 5
    @Electrux Actually this is not allowed.

    https://stackoverflow.com/questions...

    Haven't tried that with headers utilizing
    extern "C" {}
    though...
  • 1
    @Yamakuzure ah... that makes sense... thanks for telling ❤️
  • 1
    Beautiful
  • 2
    @Yamakuzure In this case, it should be allowed. ncurses isn't part of the standard library and nothing references it except my own program. Although there are a lot of things that could still go wrong here, and I realize this is terrible and awful to do. Which is why, after compiling and testing a bit, I reverted this change. But it's incredible that the language allows you to do this.
  • 0
    you could simply create a header file on your own which in cludes the ncurses header and mimics its api (ofc only the part you are using. otherwise it is a hell of a job to do on a project wide basis) as static functions. this way you have the same syntax: `ncurses::foo(bar)` without breaking external dependencies

    disclaimer: i had no need for this yet so i dont know if it would work or if it is allowed
  • 2
    @AlgoRythm It works in your case, because ncurses.h wraps the API into

    #ifdef __cplusplus

    extern "C" {

    #define NCURSES_CAST(type,value) static_cast<type>(value)

    #else

    #define NCURSES_CAST(type,value) (type)(value)

    #endif

    (...)

    #ifdef __cplusplus

    #ifndef NCURSES_NOMACROS

    /* these names conflict with STL */

    #undef box

    #undef clear

    #undef erase

    #undef move

    #undef refresh

    #endif /* NCURSES_NOMACROS */

    }

    #endif

    So the ncurses devs are very nice to C++ devs. ;-)

    Thanks to the extern declaration no names get mangled, and the namespace name has no influence on finding the resolving of the symbols.

    (WHY does this THING of an editor puts in empty lines all over the place? *grawl* >^[ )
  • 2
    @AlgoRythm not really incredible, when you consider that #include is literally just a copy&paste operation ^^

    The preprocessor just doesn't care, because why should it?
Add Comment