iDevGames Forums
Quick C/C++ macro question - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Quick C/C++ macro question (/thread-10615.html)

Quick C/C++ macro question - sealfin - May 25, 2013 08:47 AM

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?

RE: Quick C/C++ macro question - SethWillits - May 25, 2013 10:31 AM

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.

RE: Quick C/C++ macro question - sealfin - May 25, 2013 03:56 PM

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

RE: Quick C/C++ macro question - OneSadCookie - May 26, 2013 06:31 PM

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++...

RE: Quick C/C++ macro question - sealfin - May 27, 2013 03:28 PM

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