Products > Programming

C macro to determine if a type is signed

<< < (6/6)

golden_labels:

--- Quote from: Jeroen3 on November 29, 2019, 08:42:42 pm ---Due to it not working in msvc should be an indicator that you're entering terrain that will need to carry ifdefs and erroring out on unknown compilers. Not very useful.
--- End quote ---
C is not really supported by MSVC, so it is not expected that any C code will work there. Nowadays MSVC is a Microsoft C++/CLI compiler, which can also handle C++(1). C is “supported” only as long as it more or less falls under what C++ covers.
____
(1) But I heard it may fail to reject invalid C++ code despite required to do so.

Jeroen3:

--- Quote from: Nominal Animal on November 29, 2019, 10:21:04 pm ---As I explained before, it is possible to compile and run at build time a program that generates the necessary macros for the types needed.
The downside is that this won't work when cross-compiling to a different architecture or OS.

--- End quote ---
I have written a code generator in python for some C project. It fills a linked-list from snippets defined in many files. You could go this route.
There is even a C parser (compiled C) for python if you want.

Nominal Animal:

--- Quote from: Jeroen3 on November 29, 2019, 10:59:29 pm ---
--- Quote from: Nominal Animal on November 29, 2019, 10:21:04 pm ---As I explained before, it is possible to compile and run at build time a program that generates the necessary macros for the types needed.
The downside is that this won't work when cross-compiling to a different architecture or OS.

--- End quote ---
I have written a code generator in python for some C project. It fills a linked-list from snippets defined in many files. You could go this route.
There is even a C parser (compiled C) for python if you want.

--- End quote ---
No, the issue is that the C code must be compiled and run on the target OS and architecture, not on the host, to determine the signedness.  You cannot determine the signedness of arbitrary types on the target by doing stuff on the host.  Having a code generator does not help at all there.

As to the types whose signedness is needed, that is trivial to maintain, and does not need a code generator: consider the practical use cases.

Kalvin:

--- Quote from: Jeroen3 on November 29, 2019, 08:42:42 pm ---Someone posted a solution right here. But removed it!

--- End quote ---

It was probably me. I didn't like the solution as it was *really* an ugly hack, and requires manual maintenance etc. Also, it won't work with something like
--- Code: ---IS_SIGNED(unsigned int) /* Will not work */
--- End code ---

However, here is the principle:


--- Code: ---#define char_is_signed 1
#define int_is_signed 1
#define int8_t_is_signed 1
#define uint8_t_is_signed 0
#define int16_t_is_signed 1
#define uint16_t_is_signed 0
#define int32_t_is_signed 1
#define uint32_t_is_signed 0

#define IS_SIGNED(T) (T ## _is_signed)
--- End code ---

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod