condition ? instruction1 : instruction2 ; question.

This is a discussion on condition ? instruction1 : instruction2 ; question. within the C++ Programming forums, part of the General Programming Boards category; Hello, I have such a line: Code: mapa[i][j] == '1' ? dist[i][j] = 0, Q.push(p(make_pair(i,j), make_pair(i,j))) : dist[i][j] = 0x7fffffff ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    condition ? instruction1 : instruction2 ; question.

    Hello,
    I have such a line:
    Code:
    mapa[i][j] == '1' ? dist[i][j] = 0, Q.push(p(make_pair(i,j), make_pair(i,j))) : dist[i][j] = 0x7fffffff ;
    but it doesn't really work, I got an error:
    error: `((void)(dist[i][j] <unknown operator> 0), (&Q)->std::queue<_Tp, _Sequence>:ush [with _Tp = p, _Sequence = std::deque<p, std::allocator<p> >](((const p&)((const p*)(&p(std::make_pair [with _T1 = int, _T2 = int](i, j), std::make_pair [with _T1 = int, _T2 = int](i, j)))))))' has type `void' and is not a throw-expression
    Can anyone tell me why it doesn't work ?
    This part of code which do the same works fine:
    Code:
    dist[i][j] = 0x7fffffff;
    if(mapa[i][j] == '1')
    {
            dist[i][j] = 0;
            Q.push(p(make_pair(i,j), make_pair(i,j)));
    }
    Regards,
    apacz

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    If I were you, I would use the latter form and just forget about it. The ternary operator has its uses, but obfuscating code is not one of them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    Hi,
    I don't think that it obfuscates the code but it makes it shorter.
    Regards,
    apacz

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    For the sequential evaluation operator to work effectively inside the ternary operator, you have to group expressions inside parenthesis. This is so because of the higher precedence of the ternary operator.

    But as Laserlight said, what you are doing is poor, poor, form. It's ugly and it helps no one, not even you the original programmer as you are already realizing.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    And what exactly is wrong with "long code"?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > I don't think that it obfuscates the code but it makes it shorter.
    There is no relationship between the number of characters in your source code and the number of instructions generated by the compiler.

    You still have the same number of function calls, comparisons and assignments. Rearranging them into some obfuscated mess isn't going to produce any more efficient code.

    On the contrary, highly obfuscated code is trickier to optimise than simple code - if it's hard to understand, then chances are the optimiser will have a hard time figuring it out as well and produce worse code.

    But if you really want it, then perhaps
    Code:
    dist[i][j] = mapa[i][j] == '1'
        ? Q.push(p(make_pair(i,j), make_pair(i,j))), 0
        : 0x7fffffff ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thanks for replies.
    Salem: I have a question to the middle part of this operator (Q.push(p(make_pair(i,j), make_pair(i,j))), 0). .push() doesn't return anything so this '0' is a real value of dist[][]. But how does it really work, I mean does this ternary operator looks for first available value to assign ? for example tmp = something == 1 ? Q.push(0), Q.push(1), 0, Q.push(2) : 0 ;
    And if the something == 1, then tmp will be equal to 0 ?
    Regards,
    apacz

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Look up the comma operator.
    a = foo, bar;
    The result is the right hand side of the comma operator, but the left hand side is also evaluated (for any side effects).

    > assign ? for example tmp = something == 1 ? Q.push(0), Q.push(1), 0, Q.push(2) : 0 ;
    > And if the something == 1, then tmp will be equal to 0 ?
    No, it will be the result returned by Q.push(2)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fmod question
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 05:26 PM
  2. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  3. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21