Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Ternary operator logic

I'm having trouble figuring out what this if statement is doing. This is not my code so I am simply trying to understand what the Ternary operator is doing.

    if((model[STRIDE].isLogging == true ? model[STRIDE].value           : g_uiStride)   == g_uiStride &&
       (model[NUMVERTS].isLogging == true ? model[NUMVERTS].value       : NumVertices)  == NumVertices &&
       (model[PRIMCOUNT].isLogging == true ? model[PRIMCOUNT].value     : primCount)    == primCount &&
       (model[STARTINDEX].isLogging == true ? model[STARTINDEX].value   : startIndex)   == startIndex)
    {
like image 512
user1143896 Avatar asked Mar 01 '26 17:03

user1143896


1 Answers

First,

(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride

could be written:

(model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride

the ternary

model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride

checks to see if model[STRIDE].isLogging is true. If it is, it takes the value model[STRIDE].value. If not, it takes the value g_uiStride. This is then compared to g_uiStride.

So, if it isn't logging, then this portion is automatically true because g_uiStride is compared to itself. If it is logging, it is true if mode[STRIDE].value == g_uiStride and

like image 130
vextorspace Avatar answered Mar 03 '26 06:03

vextorspace