[libc++abi] use of <stddef.h> vs. <cstddef> etc.

Dear developers,

I'm new to LLVM, so please don't bite off my head if this is a dumb question.

I've noticed that some of the C++ files (both headers and bodies) include C-headers, like <stddef.h>, <assert.h> etc., while others use the corresponding C++-headers, like <cstddef>, <cassert>, and so on.

So, I was wondering the reason for that might be: Is this just out of habit and custom, or is there more to it?

Best regards,

Flo.

Hi Florian

Dear developers,

I'm new to LLVM, so please don't bite off my head if this is a dumb
question.

I've noticed that some of the C++ files (both headers and bodies) include
C-headers, like <stddef.h>, <assert.h> etc., while others use the
corresponding C++-headers, like <cstddef>, <cassert>, and so on.

So, I was wondering the reason for that might be: Is this just out of
habit and custom, or is there more to it?

Best regards,

Flo.

I think in general you'd want to use the C++ headers, not the C headers
I.e. prefer cstdio over stdio.h.
One exception is when coding inside of libcxx itself (as opposed to using
it). There you are usually implementing the C++ header itself and including
the C header is often the necessary thing to do for one reason or another.
Sometimes though it might be it's own C++ header that it needs to reference
depending on the case in question.
You might of course want to reference the C header if the header file is
going to be compiled by a C compiler instead of a C++ one.

Generally it's quite a common case is that people use the C headers in C++
by mistake or occasionally because of some compatibility problem they are
trying to avoid. The Ninja project is one where I see a lot of C references
that I think could be changed to C++ but that was actively refused when I
suggested it. So I don't know all the reasons why people do this but it's
quite common. I'd generally stick to the rule if your code is in a c++
program and you're not aware of any problem or trying to share the header
with a C program, use the C++ headers, sometimes they provide useful
overloads of similar functions in the C headers. Sometimes they can
conflict too (he says with one such example in mind, coming to a forum near
you soon lol)

Hi G M,

thank you for your quick response.

G M wrote:

Hi Florian

Florian Kutscherauer wrote:

I've noticed that some of the C++ files (both headers and bodies)
include C-headers, like <stddef.h>, <assert.h> etc., while others use
the corresponding C++-headers, like <cstddef>, <cassert>, and so on.

So, I was wondering [what] the reason for that might be: Is this
just out of habit and custom, or is there more to it?

I think in general you'd want to use the C++ headers, not the C
headers I.e. prefer cstdio over stdio.h.

I agree, and that is precisely why I was wondering.

One exception is when coding inside of libcxx itself (as opposed to
using it). There you are usually implementing the C++ header itself
and including the C header is often the necessary thing to do for one
reason or another. Sometimes though it might be it's own C++ header
that it needs to reference depending on the case in question.

Of course. And in such a case I fully understand the reasons for including a C-header. What puzzled me was the use of those C-headers in C++-files outside of libc++ (in this case: libc++abi).

You might of course want to reference the C header if the header file
is going to be compiled by a C compiler instead of a C++ one.

Generally it's quite a common case is that people use the C headers in
C++ by mistake or occasionally because of some compatibility problem
they are trying to avoid.

The reason for asking was to find out precisely which one of those caused the C-headers in libc++abi to be used.

The Ninja project is one where I see a lot of C references that I
think could be changed to C++ but that was actively refused when I
suggested it.

Some people are truly religious about their coding-styles...

So I don't know all the reasons why people do this but it's quite
common. I'd generally stick to the rule if your code is in a c++
program and you're not aware of any problem or trying to share the
header with a C program, use the C++ headers, sometimes they provide
useful overloads of similar functions in the C headers.

My thoughts, exactly.