2

Let's say I Have a header file test.h.

#pragma once

extern uint64_t a;

void foo( uint64_t );

In my case, uint64_t is used to represent a Bitboard as a part of my chess engine
test.h
Naturally I will be using uint64_t everywhere. I wanted to create a type alias for uint64_t, as Bitboard.
So I did

using uint64_t = Bitboard;

But since this is a header file, Bitboard is now defined everywhere, since this header file is used by almost all the other files of the project.
The problem is that I only want to use this alias in test.h.

The problem

The project isn't small, and Bitboard isn't a very unique identifier, I feel a global alias like this can cause some collisions, and hence I want to strictly keep it within test.h.

Is there any way I can still use create something in a header file, and not have it leak into all the other files of my project?

9
  • 3
    You say this causes a problem, but you haven't really said what the problem is. I don't see why it should be a problem. Anyway, you could use namespaces. I also suggest you use typedef instead of using. A question to ask yourself is why this header is used by all the rest of your project. Maybe you have an organizational problem. Commented Nov 2, 2020 at 4:45
  • @paddy I recently got criticized for using typedef, and not using. I have organized functions into namespaces, but I do want something like a type alias to be only visible in the source of my header file and not any other file that might use it! I will edit the question and explain the issue. Commented Nov 2, 2020 at 4:49
  • That doesn't make sense. What about the source files where you implement the functions declared to use this type? Should they still use uint64_t? Or what about the code calling these functions using variables? Commented Nov 2, 2020 at 5:02
  • What is the reason you want to "hide" this type outside the header file? Wha problem is that supposed to solve? Right now this is really too much of an XY problem. Commented Nov 2, 2020 at 5:05
  • 1
    Regarding name collisions, since youre programming in C++, the natural solution would be to use namespaces. Or encapsulate your code in classes. Commented Nov 2, 2020 at 5:28

3 Answers 3

2

Best you can do is use an ugly C #define

#pragma once

#define Bitboard uint64_t

extern Bitboard a;
void foo(Bitboard);

...

#undef Bitboard

Sign up to request clarification or add additional context in comments.

5 Comments

This is very useful, may I ask why you would consider this to be "Ugly"?
If you want ideas for a simple chess engine you can look at my own(shameless plug). It's pretty simple and concise.
@AryanParekh #define statements literally string-replace text before the compiler runs, so this can sometimes cause unexpected problems. If you use it on a small context and then use #undef it's usually ok.
I have been working on this one for a while now, Bitboards is the only real challenge, then comes the evaluation.
@AryanParekh. That's true. But evaluation is boring, so why not just do the fun part?
2

Is there any way I can still use create something in a header file, and not have it leak into all the other files of my project?

No. Included files are included entirely. If an included file contains something, then that something will be included. Simple solution is to not put something into the header that you don't want it to contain.


using uint64_t = Bitboard;

But since this is a header file, Bitboard is now defined everywhere

That doesn't define Bitboard. That defines uint64_t - which is an identifier reserved to the language implementation in the global namespace.


The project isn't small, and Bitboard isn't a very unique identifier

Besides the solution of not defining it mentioned above, a way to deal with this is to define the name within a namespace so that its uniqueness improves.

2 Comments

Hello, thanks for your answer! That is exactly what my problem is though.
@AryanParekh The solution to the problem is to remove the thing from the header that you want to not be included.
1

Move it into a new header file, one that will be included only by the source files you want privy to the secret.

Comments

Your Answer

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