C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Use of volatile as function argument should not be deprecated.

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 09 Jun 2022 12:30:57 -0700
On Thursday, 9 June 2022 10:28:13 PDT Jason McKesson via Std-Proposals wrote:
> OK, I'm a normal C++ programmer and I read over that code. My first
> thought will be "why is there a pointless variable here?" The idea
> that the presence of `if(b)` should change *anything* about the
> execution of this code is absurd. This only makes sense to those who
> already know way too much about the language.

BTW, a similar problem occurs with interposition of global functions. Some
architectures (notably, ELF-based ones) allow a function to be replaced with
another implementation.

The standard says this is an ODR violation and thus IFNDR. But because it says
it's ill-formed, the compiler is allowed to assume you don't do it.

https://github.com/qt/qtbase/blob/6.3/src/corelib/kernel/
qcoreapplication.cpp#L258-L260
extern "C" void Q_CORE_EXPORT qt_startup_hook()
{
}

This function is called from the same .cpp. Is the compiler allowed to
suppress the call to this function, since it "clearly does nothing"?

https://gcc.godbolt.org/z/dTEK5TvdT

Two of the four did. We can exclude MSVC here because there's no such thing as
interposition on Windows. This behaviour is controllable with a compiler
switch, though: GCC will suppress the call with -fno-semantic-interposition
and Clang will cease suppressing it with -fsemantic-interposition. Both also
appears to understand that the function is meant to be overridden if it's
weak, which is the solution I adopted for Qt 6.4:

https://gcc.godbolt.org/z/nbGW6aGWM

The point here is that trying to do things that are outside of the language
require solutions equally outside of the language. Just because volatile used
to work for something it wasn't meant to doesn't mean it was a correct use of
it. Simply reverting doesn't help. Either write a paper to codify the
behaviour, or ask for a compiler extension like the weak attribute above.

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

Received on 2022-06-09 19:30:59