Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Incorrect result type from ternary operator

nemequApr 22 2018 — edited Apr 25 2018

According to the C specification (3.3.15 in C89, 6.5.15¶6 in both C99 and C11), "if one operand is a null pointer constant, the result has the type of the other operand". This doesn't appear to be the case for suncc. Here is a test using _Generic which works in other C11 compilers but not suncc:

#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 201112L)

#  error Requires C11

#endif

#include <assert.h>

#include <stddef.h>

#include <stdint.h>

#define IS_CONSTEXPR(expr) _Generic((1 ? (void*) (intptr_t) ((expr) * 0) : (int*) 0), int*: 1, void*: 0)

#define REQUIRE_CONSTEXPR(expr) (IS_CONSTEXPR(expr) ? (expr) : -1)

int main(int argc, char** argv) {

  int foo[REQUIRE_CONSTEXPR(42)];

  assert(IS_CONSTEXPR(42) == 1);

  assert(IS_CONSTEXPR(argc) == 0);

}

There is some more information on how and why this works in C11 at https://stackoverflow.com/questions/49480442/detecting-integer-constant-expressions-in-macros/49481840

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 23 2018
Added on Apr 22 2018
1 comment
374 views