"C4127: conditional expression is constant" Huh?

This is a discussion on "C4127: conditional expression is constant" Huh? within the C++ Programming forums, part of the General Programming Boards category; Is my compiler crazy? (OK, it's VC++ 6.0, so that might be a trick question). Code such as the following ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    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.
    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

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 2 12 LastLast
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, 08: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, 09:34 AM

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