##if USING macro

29 messages · Page 1 of 1 (latest)

clear brook
#

Hello, I'm trying to come up with a way to have an #if USING(SOME_PREPROCESSOR),

SOME_PREPROCESSOR would be either defined as 0, or 1, but the main goal is that I want the statement to be a compile-time error if SOME_PREPROCESSOR isn't defined at all, the #if USING would also need to check the value, which would either be the 0 or 1.

I've tried a few things like the below, but can't seem to get it to work.
Thanks!

#define USING(X) (X && X)
flat krakenBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

wild rose
#

defined checks if a preprocesor macro is defined

#

there's also ifdef

#

So you could do:

#if defined(X)
// what you want to do with it
#else
#error X not defined
#endif
clear brook
#

That isn't quite what I was looking for, but I probably worded the question wrong. I don't just want to know if the thing exists or not, I want to check the value

wild rose
#

And error if the macro doesn't exist?

#

or just silently fail?

clear brook
#

If it is not defined, correct

#

I thought that

#define USING(X) (X && X) would work, becuse it would just evaluate to #if &&, but I was wrong

wild rose
#

Oh, do you mean function like macros?

#

So if you don't pass X to USING it just emits &&?

clear brook
#

No, hmmm. Let me get a better example

wild rose
#

yes please. also, i'm generally against macros, so there's probably a better way within the C++ language itself if you can share the actual problem you're trying to solve

clear brook
#

The main goal is to just protect against free-standing macros and avoid spelling mistakes.

For example me code defines WITH_EDITOR=0 (or 1), and I want to protect against accidentally doing WITH_EDTIOR or something

#

For example, I'm trying to just make a simple helper to protect against these sort of cases. Just making a more strict check.

#

The first one should error, as WITH_EDTIOR is not defined

wild rose
#

I mean, you could alway do:

#if defined(WITH_EDITOR) && WITH_EDITOR == 0
// Do some stuff if the editor is defined to 0
#elif defined(WITH_EDITOR) && WITH_EDITOR == 1
// Do some stuff if editor is defined to 1
#else
// Do some stuff if it's not defined or defined to something else
#endif
#

I'd just build out simple macros based on that

#

So like:

#define DO_WITH_EDITOR defined(WITH_EDITOR) && WITH_EDITOR == 0
clear brook
#

I'm fairly certain that macro expansion producing 'defined' has undefined behavior :/

compact epoch
#

#if replaces undefined macros with 0, not empty, that's why your X && X doesn't error

clear brook
#

Yeah :/

#

Okay I figured it out.

#define IN_USE ||
#define NOT_IN_USE &&
#define USING(X) (1 X 0)

define your type as either IN_USE or NOT_IN_USE

flat krakenBOT
# flat kraken

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

compact epoch
#

Here's a proper one that handles 0 and 1

#

I hate that I somehow ended up learning how to work around legacy MSVC preprocessor bugs 😛