Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@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. -
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? 🤔😯
-
@Electrux Actually this is not allowed.
https://stackoverflow.com/questions...
Haven't tried that with headers utilizing
extern "C" {}
though... -
@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.
-
vadimir2006yyou 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 -
@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* >^[ ) -
@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?
Related Rants
-
xjose97x19Just saw a variable in C named like this: long time_ago; //in a galaxy far away I laughed no stop.
-
Unskipp24So this happened last night... Gf: my favorite bra is not fitting me anymore Me: get a new one ? Gf: but it ...
-
sam966911Hats off to this lady .... I would have just flipped the machine
I ALWAYS THOUGHT YOU CAN DO THIS BUT NEVER TRIED IT
THIS IS MAGIC
random
preprocessor
c++
namespace