Thread: g++ complains while msvc doesn't

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Not so far
    Posts
    9

    g++ complains while msvc doesn't

    The following statement compiles without errors in VS2015 community:

    Code:
    pointInNumber ? scCTX.SetState(SCE_DD_DEFAULT) : pointInNumber = true;
    However, under GCC, I get the following error:
    Code:
    error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
    Also, g++ throws a warning on switch(bool) but MSVC seems to have no issues.

    Could somebody explain the differences? Am I using the conditional operator in a wrong way? Also, is switch(bool) considered a bad programming practice? If so, why?

  2. #2
    Guest
    Guest
    Am I using the conditional operator in a wrong way?
    Based on my knowledge, you are.

    The usual pattern is:
    Code:
    variable = condition ? true_option : false_option;
    Where both true_option and false_option have to be expressions or value-returning functions that can be assigned to variable. I don't know what your code is supposed to do, so I can't rewrite your example. Maybe explain your intent.

    Regarding switch(bool), it's probably because switch actually takes an integer argument. I think using if-else looks neater, any reason not to use that?
    Last edited by Guest; 09-05-2016 at 05:26 AM.

  3. #3
    Registered User
    Join Date
    May 2013
    Location
    Not so far
    Posts
    9
    Quote Originally Posted by Guest View Post
    Maybe explain your intent.
    What I wanted to do was to condense this,
    Code:
    if(pointInNumber)
        scCTX.SetState(SCE_DD_DEFAULT);
    else
        pointInNumber = true;
    into this:
    Code:
    pointInNumber ? scCTX.SetState(SCE_DD_DEFAULT) : pointInNumber = true;


    It worked in VS, so I was caught by surprise when it didn't work in GCC.

  4. #4
    Guest
    Guest
    Hmm, I'm surprised it works on MSVC but maybe I have a gap in my knowledge there. With that being said, I don't see why...
    Code:
    if(pointInNumber)
        scCTX.SetState(SCE_DD_DEFAULT);
    ...doesn't suffice on its own.

    Edit: Just tested it myself and it seems you cannot have incompatible types in each conditional expression (if SetState() returned bool, it would work). Usually GCC is right in these cases though, so I recommend you investigate the rules behind this and adapt your code, if necessary.
    Last edited by Guest; 09-05-2016 at 05:40 AM.

  5. #5
    Registered User
    Join Date
    May 2013
    Location
    Not so far
    Posts
    9
    Actually I was writing a Scintilla lexer, and that code is supposed to check if there are multiple decimal points in a number in which case the "number" is no longer considered a number and hence not styled.

    EDIT (after noticing your edit): Got it. Thanks for your help!
    Last edited by Bango; 09-05-2016 at 05:41 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    ?: has higher precedence than =

    So what you've written is this
    ( pointInNumber ? scCTX.SetState(SCE_DD_DEFAULT) : pointInNumber ) = true;

    Since the error message complains about void, then it seems obvious that you can't do scCTX.SetState(SCE_DD_DEFAULT) = true, hence the use of the ?: is wrong.

    FWIW, the use of ?: in place of the more obvious if / then / else construct was a mistake in itself.
    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.

  7. #7
    Registered User
    Join Date
    May 2013
    Location
    Not so far
    Posts
    9
    Ah that explains it.

    Quote Originally Posted by Salem View Post
    FWIW, the use of ?: in place of the more obvious if / then / else construct was a mistake in itself.
    In your opinion, when (if ever) the conditional operator should be preferred over an if/else construct?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The conditional operator is fine if you're doing

    Code:
    if (a)
        var = true_expr;
    else
        var = false_expr;
    This just amounts to var = a ? true_expr : false_expr;

    What you're trying to do is an abuse of the operator. It is means for the above construct and only that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2013, 05:33 AM
  2. pointers and arrays: my compiler complains
    By violatro in forum C Programming
    Replies: 12
    Last Post: 06-09-2010, 10:14 AM
  3. Pointer Trouble (GCC complains)
    By Ghiofish in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2009, 01:06 AM
  4. Replies: 1
    Last Post: 05-21-2006, 12:31 PM
  5. MSVC++ doesn't have graphics.h
    By scooby13q in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 10:32 AM

Tags for this Thread