C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Drop same sequence of tokens for inline

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 04 May 2023 12:57:32 -0700
On Thursday, 4 May 2023 12:24:47 PDT Tom Honermann via Std-Proposals wrote:
> Taking the address of a (inline or otherwise) function in one
> translation unit must produce a pointer equivalent to that produced in
> another translation unit; they must both point to the same function.
> Thus, you can't have different implementations in different translation
> units.

C solves this in a different way. In my opinion, C's inline is much more
cumbersome and limited, but probably was required to deal with limitations of
compilers.

Non-static inline functions in C that didn't get inlined place a call to an
out-of-line copy that the compiler will not emit for you, unlike in C++. It's
the developer's responsibility to define that. On one hand, it allows the
developer to emit more complex but more optimised implementations for that
case. On the other hand, that is hardly ever needed, but does require the
emission of that out-of-line copy whether it was needed or not.

If you take the address of such a function, you simply get the address of that
out-of-line copy. Note how there's no sign of f's presence in
https://c.godbolt.org/z/Y6a6r1j68

As for static variables inside inline functions, they are static and won't get
merged across TUs. That's probably not what you want, and yet this is not ill-
formed in C (see https://c.godbolt.org/z/Y3T7PnqKW).

In my opinion, C's implementation is a loaded machine gun pointing downwards
where your feet generally are. Experts will use that well and miss shooting
themselves in the foot, but non-experts won't. C++'s implementation is much
nicer and safer.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-05-04 19:57:33