Thread: "C4127: conditional expression is constant" Huh?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    "C4127: conditional expression is constant" Huh?

    Is my compiler crazy? (OK, it's VC++ 6.0, so that might be a trick question).
    Code such as the following gives me C4127: conditional expression is constant
    Code:
    ASSERT(m_reader.m_szSourceCurrent + nNumEndSkip >= m_szSourceCurrent);
    
    ASSERT(this == &pNode->GetParent());
    
    ASSERT(it != m_children.end());
    How can those conditions possibly be constant??

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It depends on the code
    but if for example in the line above you have initialized
    m_reader.m_szSourceCurrent = m_szSourc;

    and nNumEndSkip is unsigned int

    then first condition will be always true
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't really see how, since nNumEndSkip can be any number, but I wonder if it has something to do with this being in an inline function?
    Code:
    inline void Bookmark::GetSubString(String& strString, int nNumEndSkip)
    {
        ASSERT(m_reader.m_szSourceCurrent + nNumEndSkip >= m_szSourceCurrent);
    ...
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Doesn't this warning tell you that an expression will always evaluate to true or false (that is, there is no real point in evaluating it at all)? C4127
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but I wonder if it has something to do with this being in an inline function?
    I suppose so - compiler looks at the code there the parameters of the function are initialized - and probably makes some optimization - resulting in the conditional expression to be constant
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    Doesn't this warning tell you that an expression will always evaluate to true or false (that is, there is no real point in evaluating it at all)? C4127
    Yes, hence my confusion.

    Quote Originally Posted by vart View Post
    I suppose so - compiler looks at the code there the parameters of the function are initialized - and probably makes some optimization - resulting in the conditional expression to be constant
    I did a search for all the places where that function is called, and it looks like it always passes positive a constant as the 2nd parameter.
    I'm not sure about all the other lines with that warning, but I'm guessing it's for a similar reason.
    I hate when 3rd party code doesn't test their code with the highest warning levels...

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Sometimes optimizations throw too many warnings that are irrelevant. If its just a warning then ignore it. I leave warning on, but generally I ignore 99% of them.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Warnings are thrown because you aren't supposed to ignore them.
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Warnings are thrown because you aren't supposed to ignore them.
    ... unless they really are irrelevant. But yeah, leaving them on and then ignoring them may result in ignoring relevant warnings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But if they are irrelevant, then you should suppress them. And in most cases, many warnings are not unfounded (read: not irrelevant).
    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.

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Whereas turning them off and then ignoring ensures you miss relevant ones. By ignoring, I don't mean I don't read them the first itme, it just means mostly I take no action as they are irrelevant. e.g.

    \blah.cpp(15) : warning C4101: 'pTemp' : unreferenced local variable


    irrelevant. used for debugging purposes, so Im not about to remove its definition, but irrelevant to the proper operation fo the app. And I dont realyl have the time to put in a pragma for every warning liek this.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can just add "pTemp;" to get rid of it. Temporarily.
    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.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I somehow doubt that the compiler can emit warnings based on what parameters the function was called with. If it is warning that some condition will be always true/false, I would expect there to be something in the code that makes it so sure about it.

    It is telling you that the code as it is, is potentially wrong. It shouldn't mean that in the context of this current program, some condition is always true/false - if that might change if the function is called with different arguments. In this case it means that you might be asserting things that cannot happen because of the code logic.

    Do all those ASSERTs trigger the warning? Could you perhaps post the full function (and class declaration)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Elysia View Post
    You can just add "pTemp;" to get rid of it. Temporarily.
    And get an "expression has no effect" warning instead?

    (Debug things probably might be compiled conditionally in the first place?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by anon View Post
    And get an "expression has no effect" warning instead?
    It doesn't do that. Not in Visual Studio at the very least.
    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. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM