Quick C/C++ macro question
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...
That old C interpreter also doesn't have a bool type, which is a little more of a problem, as...
...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
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?
That old C interpreter doesn't have constant literals for true or false, which isn't a problem...
Code:
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
That old C interpreter also doesn't have a bool type, which is a little more of a problem, as...
Code:
#ifndef bool
typedef short bool;
#endif
...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

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?
Mark Bishop
--
Student and freelance OS X & iOS developer
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.
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
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
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

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

Mark Bishop
--
Student and freelance OS X & iOS developer
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++...
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 
Looks like I'll go with SethWillits suggestion of a macro value; annoying, as I was hoping that there was a more elegant solution

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

Mark Bishop
--
Student and freelance OS X & iOS developer
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Quick PHP question... | sealfin | 6 | 13,620 |
Nov 3, 2014 08:00 AM Last Post: janmr |