one else for two if possible?



  • Hi,

    my question is more a general c++ question then Qt, but i hope you can help me with that, too.

    Example 1:
    first condition: the index is not greater then the container
    2nd condition: the value in the container at the index is set
    if one of those is not given, the else should be executed.

    So it need to be like this:

    if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
    	m_drawList.push_back(new_info);
    else
    	m_drawList.push_front(new_info);
    

    problem: if the first condition is false, i cannot even test the 2nd condition, so it need to be like this:

    if (segmentIterator->textureIndex < m_materials->size())
            if(m_materials->at(segmentIterator->textureIndex).transparent)
            	m_drawList.push_back(new_info);
    else
    	m_drawList.push_front(new_info);
    

    problem: dangling else. the compiler will read it like this:

    if (segmentIterator->textureIndex < m_materials->size())
    {
    	if(m_materials->at(segmentIterator->textureIndex).transparent)
    		m_drawList.push_back(new_info);
    	else
    		m_drawList.push_front(new_info);
    }
    

    Any solutions for that?? In theory an good compiler should optimize the code and if the first condition of a && expression is false, the 2nd shouldn't be even touched. But i'm not sure if that works. Any ideas about that??



  • usa a variable...

    bool check = segmentIterator->textureIndex < m_materials->size();
    if(check) check =m_materials->at(segmentIterator->textureIndex).transparent;
    if(check) 
    m_drawList.push_back(new_info);
    else
    m_drawList.push_front(new_info);
    

  • Moderators

    @QT-static-prgm said in one else for two if possible?:

    problem: if the first condition is false, i cannot even test the 2nd condition, so it need to be like this:

    What is the problem? Why do you want to test the second condition if the first is false?
    As far as I can see the second condition does not have side effects, so it should be fine.



  • @jsulm i want to test the 2nd condition, but to prevent errors, i check the index to be not out of the range.

    btw for me works

    if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
    	m_drawList.push_back(new_info);
    else
    	m_drawList.push_front(new_info);
    

    But i'm not sure if it is compiler specific.


  • Moderators

    Your code is ok.
    Order of evaluation for && operator is specified and it is left to right. && also uses short circuit evaluation, so the right side is evaluated if and only if the left side is evaluated to true.



  • @Chris-Kawa all right, thank you. I just wasn't sure if it was only theory in my university lecture, or if it is implemented in all C++ compiler



  • @Chris-Kawa said in one else for two if possible?:

    also uses short circuit evaluation

    Disclaimer: I'm not 100% sure here

    I don't think this holds up in case of an overloaded operator&& so if transparent returns a type that has on overloaded operator&& that interacts with bool, the right part of the logical AND will be executed regardless of what the left part returns


  • Moderators

    @VRonin You're 100% right. That's a cool nitpick. I like it :D

    In this case though I doubt it matters. As you said transparent would have to be a custom class and there would have to be a Something operator&&(bool a, const SomeClass& b) defined in the scope. That's not very common.



  • Ähm yes, transparent is a boolean value of a struct.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.