Skip to main content
added 114 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50

The trick here is that static_assert can be used anywhere, even in a struct definition. sizeof return the size of the struct, which !! converts it to 1. char c exists just so that the struct is never empty. If the assertion fails, the code would not compile. If it succeeded, STATIC_ASSERT_EXPR would return 1 (true). This is better than the compiler flag because it does more than just warn, it causes compilation to fail.

0[ARRAY] instead of ARRAY[0] because in C++, trying to use ARRAY_SIZEARRAY_CARDINALITY on an object of a class for which operator[] has been overloaded would result in a compilation error, which is what one would want.

Now the only thing GNU C specific thing in your code is the statement expression, for which I know of no ISO C version. I believe the C committee is working on something similar.

The trick here is that static_assert can be used anywhere, even in a struct definition. !! converts it to 1. char c exists just so that the struct is never empty. If the assertion fails, the code would not compile. If it succeeded, STATIC_ASSERT_EXPR would return 1 (true). This is better than the compiler flag because it does more than just warn, it causes compilation to fail.

0[ARRAY] instead of ARRAY[0] because in C++, trying to use ARRAY_SIZE on an object of a class for which operator[] has been overloaded would result in a compilation error, which is what one would want.

Now the only thing GNU C specific in your code is the statement expression, for which I know of no ISO C version.

The trick here is that static_assert can be used anywhere, even in a struct definition. sizeof return the size of the struct, which !! converts it to 1. char c exists just so that the struct is never empty. If the assertion fails, the code would not compile. If it succeeded, STATIC_ASSERT_EXPR would return 1 (true). This is better than the compiler flag because it does more than just warn, it causes compilation to fail.

0[ARRAY] instead of ARRAY[0] because in C++, trying to use ARRAY_CARDINALITY on an object of a class for which operator[] has been overloaded would result in a compilation error, which is what one would want.

Now the only GNU C specific thing in your code is the statement expression, for which I know of no ISO C version. I believe the C committee is working on something similar.

added 8 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50
#define C23_PLACEHOLDER 202000L

#if define(__STDC_VERSION__) && __STDC_VERSION__ >= C23_PLACEHOLDER
    #define ATTRIB_NODISCARD    [[nodiscard]]
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
    #define ATTRIB_NODISCARD    __attribute((warn_unused_result))
#else
    #define ATTRIB_NODISCARD    /**/
#endif
#define C23_PLACEHOLDER

#if define(__STDC_VERSION__) && __STDC_VERSION__ >= C23_PLACEHOLDER
    #define ATTRIB_NODISCARD    [[nodiscard]]
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
    #define ATTRIB_NODISCARD    __attribute((warn_unused_result))
#else
    #define ATTRIB_NODISCARD    /**/
#endif
#define C23_PLACEHOLDER 202000L

#if define(__STDC_VERSION__) && __STDC_VERSION__ >= C23_PLACEHOLDER
    #define ATTRIB_NODISCARD    [[nodiscard]]
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
    #define ATTRIB_NODISCARD    __attribute((warn_unused_result))
#else
    #define ATTRIB_NODISCARD    /**/
#endif
added 221 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50

0[ARRAY] instead of ARRAY[0] because in C++, trying to use ARRAY_SIZE on an object of a class for which operator[] has been overloaded would result in a compilation error, which is what one would want.


Now the only thing GNU C specific in your code is the statement expression, for which I know of no ISO C compliant version.

Now the only thing GNU C specific in your code is the statement expression, for which I know of no ISO C compliant version.

0[ARRAY] instead of ARRAY[0] because in C++, trying to use ARRAY_SIZE on an object of a class for which operator[] has been overloaded would result in a compilation error, which is what one would want.


Now the only thing GNU C specific in your code is the statement expression, for which I know of no ISO C version.

Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50
Loading