[Interest] How does one use Q_ASSUME?

Elvis Stansvik elvstone at gmail.com
Sat May 25 11:05:39 CEST 2019


Den lör 25 maj 2019 kl 10:13 skrev René J.V. Bertin <rjvbertin at gmail.com>:
>
> Hi,
>
> I can't seem to wrap my head around what one can do with Q_ASSUME, i.e. which will be the code for which the compiler won't emit code (and how the compiler could know not to emit code as a function of a runtime condition?!)

I hadn't heard about this compiler hint, but isn't it simply that
putting in Q_ASSUME(cond), then the compiler is free in its code
generation to not generate any code to handle cases when cond is
false?

E.g.

Q_ASSUME(!atWar);

if (atWar) {
    fireNukes(); <-- Oops, nukes may not be fired, even if at war,
because compiler may have taken the hint and assumed we're not at war
}

Microsoft seems to have a nice article about their __assume (which
Q_ASSUME expands to):
https://docs.microsoft.com/en-us/cpp/intrinsics/assume?view=vs-2019

>
> Squinting at the macros it seems evident that you cannot do something like this, which to me the documentation suggests (and I think) you SHOULD be able to do:
>
> Q_ASSUME(conditionsMet, {
>   // do something that should be done only when conditions are met
> });

I don't understand. That just looks like an if statement?

Elvis

>
>
> R
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest


More information about the Interest mailing list