Thread: Ternary and commas

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Ternary and commas

    I'm having trouble using '?' in conjunction with ','

    Perhaps I'm going about it the wrong way.
    Code:
    const unsigned long long int stopwatch::read() //if rng, read() will give a current reading, if !rng, it'll return curr.
    {
    	return rng ?
    /*34*/	        (struct timeval now,
    		gettimeofday(&now,0),
    		timeval & beg = ((timeval*)data)[0],
    		now.tv_sec*100000LL+now.tv_usec) - (beg.tv_sec*100000LL+beg.tv_usec))
    		   :
    /*39*/	        curr;
    }
    Code:
    stopwatch_linux.cpp: In member function ‘const long long unsigned int stopwatch::read()’:
    34: error: expected primary-expression before ‘struct’
    34: error: expected `)' before ‘struct’
    39: error: expected `:' before ‘;’ token
    39: error: expected primary-expression before ‘;’ token
    Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I don't think you can create variables on the fly as you just did it. That makes no sense to me. Try declaring it before.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your parentheses aren't balanced -- one too many ) (don't ask me which one).

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You're right. I saw something like this online:
    Code:
    int a = (int b = 1, ++b);
    But I guess that is invalid.

    *edit* and thanks, tabstop.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    But Desolation's right, no declarations. You can have expressions, but not declarations.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In my opinion this is better written as if/else and just statements, instead of just one long thing.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And surely everyone will be happier if you write this code as:
    Code:
    const unsigned long long int stopwatch::read() //if rng, read() will give a current reading, if !rng, it'll return curr.
    {
    	if (!rng) return curr;
            struct timeval now;
    	gettimeofday(&now,0),
    	timeval & beg = *(timeval*)data;
    	return now.tv_sec*100000LL+now.tv_usec) - (beg.tv_sec*100000LL+beg.tv_usec));
    }
    With this code, you can debug it. And almost anyone can understand what actually happens (in fact, your comment "if rng, read() will ..." now becomes obsolete, as it is clear from the code that is what happens).
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whiteflags View Post
    In my opinion this is better written as if/else and just statements, instead of just one long thing.
    You and I.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I feel dumb now. I used the ternary to avoid this:
    Code:
    function
    {
       if(a)
       {}
       else
       {}
       return code_never_gets_here;
    }
    matsp's solution didn't even occur to me. Der. Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Your fears were unfounded. If you traced that, you will find that return is reached no matter which path is taken.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if you are ever in that situation again, and the pattern in my code doesn't work, then this will:
    Code:
       int ret;
       if (a)
          ret = ...
       else
          ret = ...
       return ret;
    In some circles, having a single exit from a function is actually the preferred solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thanks, matsp.

    Your fears were unfounded. If you traced that, you will find that return is reached no matter which path is taken.
    Of course it always returns, but it is inelegant, since it hides the binary behavior of the function. Everything suggested in this thread makes more sense than what I was avoiding.

    *edit* Perhaps we misunderstood each other. In my pseudocode, there should have been a 'return' within each bracket set.
    Last edited by CodeMonkey; 01-04-2009 at 05:50 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whiteflags View Post
    Your fears were unfounded. If you traced that, you will find that return is reached no matter which path is taken.
    Yes, and even simple compilers will figure such a simple case out and not say "some paths may not return a value".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by matsp View Post
    With this code, you can debug it.
    QFE!

    Note that the code used is C++, hence the struct keyword in the variable declaration is not necessary.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed