0

I am using the <conio.h> header file, and somewhere else in my source code I define a function with the name getch and it has to have that name. Since there already is a getch in <conio.h>, and this header file declares all of its functions in the global namespace, I get a name collision.

I found that using the following syntax would avoid the collision:

namespace some_namespace
{
 #include <conio.h>
}

Then I can use some_namespace::getch when I want to access the function in <conio.h> and getch when I want to access my own function.

Is this valid syntax? I know <conio.h> is only for windows, but is this kind of syntax going to behave the same across all the compilers? What other ways do you suggest to get around this problem?

Edit:

I use GCC and MSVC 2019 on Windows and it compiles fine on both of them.

I can access the functions in <conio.h> as well, getch in particular as I showed above (even though I should use the name _getch instead of getch in MSVC).

asked May 8, 2021 at 15:14
5
  • 3
    Why not put your own code (including your getch) in your own namespace? Commented May 8, 2021 at 15:17
  • 1
    Don't forget to extern "C" as well, <conio.h> is a C header file. Commented May 8, 2021 at 15:18
  • @S.M. I tried it with both gcc and MSVC 2019 and it works on both of them. Commented May 8, 2021 at 15:26
  • 2
    You should put your getch() in your namespace. Commented May 8, 2021 at 15:46
  • 1
    The standard headers were written with the intention that they be used in the global namespace. If this kind of hackery happens to work it's only by accident, and it could well fail with a different compiler (including a future version of the compiler you're using now). Commented May 8, 2021 at 16:06

1 Answer 1

1

System header files like <conio.h> which are intended to be used in both C and C++ will enclose their declarations in an extern C scope, forcing C linkage for all that is contained in them, regardless of whatever additional C++ namespaces you add. That’s why your code compiles in this case.

See also this, which is almost a duplicate of this question, but not exactly.

In short, yes it’s valid, but I would strongly encourage against it. There are many other ways to solve this problem, like creating wrapper modules for the functions you want to have alternate symbols for.

As a side note: try putting your own getch in an extern C block. My guess is you’ll get a linker error.

answered May 8, 2021 at 16:31
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.