POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Days 1-5 Server Time
29 Jul 2024 12:15:17 EDT (-0400)
  Re: Days 1-5  
From: Warp
Date: 17 Apr 2012 10:26:39
Message: <4f8d7d9f@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> Now /that/ actually makes sense. Where are these typedefs located? Do 
> you have to include them specifically, or...?

  IIRC they are in <cstdint>.

> I had hoped for maybe a compile-time warning. I haven't actually tried 
> it to see if I get one though...

  Some compilers will give you a warning if they think that a variable
is being used uninitialized, but given that the general problem of
determining that is unsolvable, it's not fool-proof.

  There are some tools (such as valgrind) that will tell you at runtime.

> Aren't expressions guaranteed to execute left-to-right?

  Expressions with side-effects can happen in any order. The compiler is
free to optimize expressions in any way it likes (as long as the result
is always the same if the expression had no multiple side-effects on the
same variable).

  For example, if you do a "table[i++] = i;" the compiler can decide to
increment the 'i' before it assigns it to that location, or after the
entire statement is done. The general solution to that is: Don't do it.

> >    Even if it isn't UB, better not do it like that.

> But that's just it. All C programmers always code in this kind of style. 
> It's part of what makes C so terrifying to try to read.

  C programmers (at least competent ones) do not have multiple side-effects
on the same variable inside the same expression. They may have multiple
side-effects on *different* variables, which is ok. For example:

    while(*dest++ = *src++) {}

  There are two side-effects there, but they are operating on different
variables, so there's no ambiguity.

> I know that zero is one thing, and everything else is the other thing. 
> But I always struggle to remember whether zero means true or false. The 
> solution, of course, is to never ever use integers as bools.

  What do you think this will print?

    bool b = true;
    std::cout << b << std::endl;

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.