What is the performance implication of converting to bool in C++?

前端 未结 11 884
后悔当初
后悔当初 2020-12-03 02:36

[This question is related to but not the same as this one.]

My compiler warns about implicitly converting or casting certain types to bool whereas explicit

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 02:58

    The performance is identical across the board. It involves a couple of instructions on x86, maybe 3 on some other architectures.

    On x86 / VC++, they all do

    cmp DWORD PTR [whatever], 0
    setne al
    

    GCC generates the same thing, but without the warnings (at any warning-level).

提交回复
热议问题