Providing C99/C11 features from C++11 and above

In C++11 and above, C99/C11 features should be available even when using C++. For example, FLT_HAS_SUBNORM should be provided by <float.h>:

$ cat <<EOF | clang++ -xc++ -std=c++11 -
#include <float.h>

#ifndef FLT_HAS_SUBNORM

error “FLT_HAS_SUBNORM is missing”

#endif
EOF

This should also work when including <cfloat>. However, this currently:

  • fails in -std=c++11 or above
  • succeeds in -std=gnu++11 or above
  • succeeds in -std=c11 or above
  • succeeds in -std=gnu11 or above

If I understand correctly, the problem is that we currently have the following in clang/lib/Headers/float.h:

#include_next <float.h> // pick up whatever the system provides


#undef FLT_HAS_SUBNORM // undefine what the system provides

// redefine our own version of the macro
#if STDC_VERSION >= 201112L || !defined(STRICT_ANSI)

define FLT_HAS_SUBNORM FLT_HAS_DENORM


#endif

The #if block checks whether we’re compiling in C11 or above, or whether we’re compiling in a non-strict dialect (basically some any gnuXXX dialect). However, it doesn’t check for strictly conforming C++, and as a result we don’t get the definitions. I’d like to know what’s the right strategy for providing these definitions in C++, and whether there’s an established way of doing this already. Naively, changing the #if to

#if STDC_VERSION >= 201112L || !defined(STRICT_ANSI) || (defined(__cplusplus) && __cplusplus >= 201103L)

define FLT_HAS_SUBNORM FLT_HAS_DENORM


#endif

seems to do the job. But I’m not very familiar with how Clang’s headers are imbricked into libc++ and how they interact with per-platform headers, so I’m asking here.

Thanks,
Louis

Actually, I just found precedent in limit.h and other headers: https://github.com/llvm-mirror/clang/blob/master/lib/Headers/limits.h#L92.

I think that answers my question. I’ll submit a Phab review and we’ll see then :slight_smile:

Louis