C++ Logo

std-proposals

Advanced search

Re: [std-proposals] unimplemented attribute

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 19 Mar 2025 15:57:24 -0700
On Wednesday, 19 March 2025 14:02:18 Pacific Daylight Time Hans Åberg via Std-
Proposals wrote:
> But one would want to make an implementation that is disabled when the
> function declaration later on gets “unimplemented” removed.

We already have that, it's called the buildsystem. We don't need to move
everything in to the C++ language.

> > ```
> > static_assert(unimplemented(bar));
> > void foo();
> > static_assert(unimplemented(bar) == false);
> > ```
>
> The way I think of it, only the function with “unimplemented” is marked as
> such; it does not propagate to functions calling it (also see below). So in
> your example, the function is always implemented even though calling an
> unimplemented function. I haven't thought about static_assert, though.

This implies that you cannot have written bar() in the first place, because it
would produce a warning-turns-into-error.

> > Another thing is, this is asking for ODR violations as you grab the
> > header with `bar` but forget one with the definition of `foo`.
> > This could easily change code `if constexpr (unimplemented(bar))`.
> > Probably any way of quarrying `unimplemented` or SFINAE it should be
> > banned.
> >
> > Another problem is how far it can propagate.
>
> Not at all if it is possible to take the function pointer of an
> unimplemented function and the linker fills it in if it discovers an
> implementation.

Too late. Warnings turn into errors, so the linker is never run.

What you're thinking of are weak functions. You can take the address of such
functions and you may get a null pointer, so you can write code such as:

[[gnu::weak]] void func();
void f()
{
     if (func)
        func();
    else
        fallback();
}

With LTO or a sufficiently smart linker, it could detect that "func" is indeed
defined and suppress the check and the else branch.

But this is not possible in all platforms. So it couldn't be part of the C++
standard.
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2025-03-19 22:57:27