iDevGames Forums

Full Version: Quick C/C++ macro question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay, I'm throwing together code which I'd like to be portable/need no changes to compile using both a very old C interpreter and using up-to-date g++.

That old C interpreter doesn't have constant literals for true or false, which isn't a problem...

#ifndef true
#define true 1

#ifndef false
#define false 0

That old C interpreter also doesn't have a bool type, which is a little more of a problem, as...

#ifndef bool
typedef short bool;

...results in g++ complaining that I'm "redeclar[ing] C++ built-in type 'bool'", which is annoying as eg. "#ifndef int" functions as I expected. Edit: no, it doesn't function as I expected, I managed to bugger up the test Blush

At the moment I'm circumventing the problem using "#ifndef __cplusplus", but I'm wondering whether there's a proper way of checking for the existence of a type?
I needed something similar the other day.

As far as I know and as far as my googling got me, the answer for directly checking if a type exists is that it's not possible since the preprocessor doesn't know C. You'd have to define a different macro value in your build settings or find one that's different between the two build environments.
Thanks for that.

I've tried searching for a solution to this problem but didn't receive any relevant results (no, I'm not interested in C++ RTTI...) so either the question I'm asking has never been asked before - which I think highly unlikely - or I suck at searching Blush

I had thought (hoped) that C++ might have additional pre-processor statements for checking for the existence of a type, statements which would be unknown to and thus ignored by the C interpreter (assuming that the C interpreter conforms to that part of the specification - and I don't have high hopes about that...) Looks like I'm not that lucky Cry
Preprocessing happens before there's any such thing as types, so there's no such possible check.

And, if you have up-to-date GCC, why wouldn't you at least compile as C... make your life a little easier than trying to make the same code compile as C and as C++...
Thanks for that - I recognise that the preprocessor wouldn't be able to be aware of a typedef'd type, but I wasn't aware that the preprocessor would be unaware of a primitive type Annoyed

Looks like I'll go with SethWillits suggestion of a macro value; annoying, as I was hoping that there was a more elegant solution Annoyed
Reference URL's