hedley icon indicating copy to clipboard operation
hedley copied to clipboard

Add HEDLEY_TYPEOF

Open Quuxplusone opened this issue 2 years ago • 0 comments

I was pointed to your project from StackOverflow (commenter says you're his "fav project" 😄) as I was looking for well-tested preprocessor detection of __typeof. https://stackoverflow.com/questions/73848714/detect-if-a-c-compiler-supports-typeof-and-or-decltype

I see you started working on __typeof support a few years ago, but never committed it to trunk: https://github.com/nemequ/hedley/commit/9c3e8420cdb1ce1f89fcf70f07281e92df176f98#diff-b01d6052731bb4f6e3cf6dfb281442574769d119caa48f61a422e05f9b540f8a

Some caveats:

  • In C, int i; __typeof((i)) is int. In C++, decltype(i) is int but decltype((i)) is int&. Therefore, you'd either have to document that the C++ version must be used only on identifiers, or (better?) you'd have to #define HEDLEY_TYPEOF(...) typename std::remove_ref<decltype(__VA_ARGS__)>::type.
  • In GCC and Clang and EDG and also in C23 if I understand correctly, const int ci = 1; __typeof(ci) is const int, i.e., __typeof does not strip cv-qualifiers. On ChibiCC, __typeof__(ci) is int.
  • I noticed that ChibiCC supports __typeof__ but not __typeof. Most compilers support both.
  • I noticed that VBCC "supports" __typeof(x), but it's a builtin that returns an int value (1=char, 3=int, 6=float, etc.) instead of the GNU C version that yields a typename.

Quuxplusone avatar Sep 28 '22 14:09 Quuxplusone