-7

Why is this valid:

int main()
{
 double good; good++;
 return 0;
}

but this is not:

int main()
{
 double good++;
 return 0;
}

I know that normally you want to initialize variables to some value before increment it (because then it'd just contain random garbage from memory), but I'm just curious why the latter does not allow this. Doesn't the post-increment operator, happen after the value is returned? It would make sense for ++good not to work, but I don't understand why good++ doesn't.

Futuza
  • 144
  • 8
  • 5
    Because declarations and definitions aren't general expressions. Also in the case of `double good++;`, remember that non-static local variables are uninitialized and have an indeterminate value, so using them in any way would lead to undefined behavior anyway. – Some programmer dude Aug 10 '16 at 17:27
  • 3
    Do note your first example is not good. It is UB as `good` is never initialized. – NathanOliver Aug 10 '16 at 17:28
  • 2
    The first isn't valid either. – molbdnilo Aug 10 '16 at 17:29
  • What do you expect the values of `good` to be in either of your examples? – GManNickG Aug 10 '16 at 17:33
  • It compiles on GCC 4.9.2...I'm not asking why it isn't a good idea to do this (I already know that), I want to know why the first compiles and the second does not. @GManNickG some random value in memory incremented by 1. – Futuza Aug 10 '16 at 17:34
  • you shouldnt expect anything from UB other than UB – 463035818_is_not_an_ai Aug 10 '16 at 17:34
  • The first compiles because it is ***sytactically*** valid C++, even if it leads to undefined behavior. The compiler can (and will if you enable more warnings which I recommend) emit a warning for the first case though. The second is simply not valid according to the C++ specification. – Some programmer dude Aug 10 '16 at 17:35
  • 1
    What would you expect `double good++;` to be? – NathanOliver Aug 10 '16 at 17:37
  • @Joachim Pileborg, That's what I'm wondering about in particular, why the c++ specification allows the first, but not the second. Do you have a source that I can read up on that more? – Futuza Aug 10 '16 at 17:40
  • @NathanOliver I am not expecting any particular value, I just expect it to be initialized to some random garbage value in memory and then that value to be incremented by one. – Futuza Aug 10 '16 at 17:42
  • 2
    @DarthFutuza What would be the use case for such sillyness? – πάντα ῥεῖ Aug 10 '16 at 17:44
  • @DarthFutuza What possible purpose would that serve? – NathanOliver Aug 10 '16 at 17:44
  • 1
    @DarthFutuza You can always [read the draft of the upcoming C++17 standard (PDF)](http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/n4594.pdf). Declarations have not changed in the new specification to allow (or disallow) silliness like this. – Some programmer dude Aug 10 '16 at 17:44
  • 1
    @DarthFutuza If you want random numbers use one of the standard random number generators. – πάντα ῥεῖ Aug 10 '16 at 17:45
  • I'm not asking to solve a practical problem, I am just trying to understand why the standard is the way it is. – Futuza Aug 10 '16 at 17:46
  • 2
    Its pointless so why even bother. Somewhere in the grammar it will spell it out but do you really need it spelled out? `double good++;` is nonsense and should not be allowed. The first code block should also not be allowed but it is a much more difficult problem to find in all cases so it is not, not required to compile. – NathanOliver Aug 10 '16 at 17:50
  • @NathanOliver because I'm curious and I feel the same way. Why should the first be allowed and not the second? They both lead to generally bad coding practices and I can't see any practical use for allowing either. I thought maybe there's a reason for the first to be allowed, but from what it sounds like there isn't one. – Futuza Aug 10 '16 at 17:56
  • The first one is legal grammar so it is allowed. It is UB but finding it can be very hard so it The standard labeled it as UB and no diagnostic is required. – NathanOliver Aug 10 '16 at 17:58
  • 1
    It appears `double good++;` is supposed to make `good` even "better" than `double`? In that case, `long double good;` is what you really want ;) – fredoverflow Aug 10 '16 at 17:59
  • Languages aren't designed in the "everything is possible, okay now what's not allowed" fashion. They're designed in "nothing is possible, okay now what will we allow" fashion. It doesn't exist because nobody made it so. People probably won't bother to take the time to make it so because there already exists ways to get that result trivially. – GManNickG Aug 10 '16 at 20:49

1 Answers1

2

You practically already answered your question: Because declaration does not assign a value. Therefore your second sample never makes sense.

The first sample consists of two separate statements each of which could make sense in a certain context. Therefore it compiles.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • Thank you, this actually brings a lot of sense to it. The first is allowed because separately the statements are valid, because of their potential use - but together do not do anything useful. Multiple statements generally aren't context sensitive, which is my bad in assuming they would be. – Futuza Aug 10 '16 at 17:59