On Mon, 14 Apr 2025 at 14:47, Julian Waters <tanksherma...@gmail.com> wrote:
>
> Hi Jonathan,
>
> Thanks for the suggestion, it seems promising. I switched out the
> error attribute for the warning attribute at first, since they should
> be equivalent except warning just warns instead of erroring. This
> results in the link step failing if LTO is enabled for some reason
> though. I then changed it to deprecated instead of warning, since
> that's available on all compilers we support, but that breaks on clang
> and VC because they seem to not do so well with redeclaring standard
> library functions. I only have one card left up my sleeve, and that's
> to use decltype:
>
> [[deprecated("use os::malloc")]] decltype(::malloc) malloc;
>
> Does ISO C++ allow you to redeclare something from the standard
> library like this? Or is it still subject to the same ambiguity about
> whether redeclaring C functions in the global namespace is allowed or
> not that you mentioned before?

I don't think that changes anything.

The C++ standard explicitly forbids adding declarations to namespace std:

"Unless otherwise specified, the behavior of a C ++ program is
undefined if it adds declarations or definitions to namespace std or
to a namespace within namespace std."

But ::malloc is not in namespace std, so that rule doesn't apply. But
that's arguably a defect in the standard caused by forgetting that
std::malloc might actually be defined as ::malloc. Re-declaring
::malloc is probably against the spirit of the rule, even if not
strictly breaking it.

Reply via email to