Author Topic: An 'interesting' thing you can do in C++  (Read 18468 times)

0 Members and 1 Guest are viewing this topic.

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3137
  • Country: ca
Re: An 'interesting' thing you can do in C++
« Reply #25 on: September 11, 2018, 02:45:47 pm »
Code: [Select]
while (young) make(), mistakes();

Code: [Select]
while (make(), mistakes(), !dead);

 
The following users thanked this post: hans, GeorgeOfTheJungle, newbrain

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26757
  • Country: nl
    • NCT Developments
Re: An 'interesting' thing you can do in C++
« Reply #26 on: September 11, 2018, 02:48:03 pm »
And it's single lines of code like that, that make it damn near impossible to debug.  When you go back two years later, it's not obvious what is happening
I fully agree. Using these kind of constructs should be banned.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: TomS_

Offline HoracioDos

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: An 'interesting' thing you can do in C++
« Reply #27 on: September 11, 2018, 02:53:06 pm »
Yep. That's clear and unambiguous and compiles without warnings. C++ is obscure, a mine field. Sometimes I wonder if Stroustrup eats mushrooms.
I'm not a C++ programmer but one of his quotes would give me goosebumps. "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off"
 
The following users thanked this post: GeorgeOfTheJungle

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3137
  • Country: ca
Re: An 'interesting' thing you can do in C++
« Reply #28 on: September 11, 2018, 03:02:08 pm »
I fully agree. Using these kind of constructs should be banned.

By whom?
 

Offline dferyance

  • Regular Contributor
  • *
  • Posts: 178
Re: An 'interesting' thing you can do in C++
« Reply #29 on: September 11, 2018, 04:08:53 pm »
Whenever I've seen people attempt to ban language features, it never really worked well. Some features make sense in certain cases but don't in others. What constructs one developer thinks are horrible, often another developer finds straightforward.

I've known programmers who like massive if/else chains to assign variable values. While I prefer simple variable assignment, sometimes with a ternary operator. I find the latter much simpler to reason about and understand as it is a formula instead of a large logic chain. But other developers like to think of it in terms of conditional statements and logic chains. Who's right here? Really there isn't a right or wrong.

Its not possible to dictate good code through some codified rules. Good programming takes a lifetime to master. It would be nice if all it took was a few rules. There are very few if any language constructs I would say never to use. Unless something else fully replaces it, they often have their purpose. But it is important to use them as clearly as possible. All language constructs can be abused.
 

Offline taydin

  • Frequent Contributor
  • **
  • Posts: 520
  • Country: tr
Re: An 'interesting' thing you can do in C++
« Reply #30 on: September 11, 2018, 04:22:33 pm »
Compared to other languages, C/C++ has very few rules. Successful programmers adopt a set of "best practices" and follow those. The MISRA recommendations, and Scott Meyers "effective C++" recommendations are good starting points for establishing ones own best practices.

If one uses all the freedom that C/C++ gives, the result will be a unmaintainable mess, a money pit.
Real programmers use machine code!

My hobby projects http://mekatronik.org/forum
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9886
  • Country: us
Re: An 'interesting' thing you can do in C++
« Reply #31 on: September 11, 2018, 04:41:22 pm »
Mentioned above and bearing repeating, it remains an exercise to determine which C compilers produce predictable output for some of these more obscure constructs.  What happens when the latest and greatest kicks some of these constructs to the curb?

I know what ++var and var++ do when they are either on the right or left side of the '=' sign.  I wouldn't want to predict what would happen if they are on both sides.  I know what I think they should do but I wouldn't want to bet that any particular compiler will do what I think.  Moreover, I wouldn't want to predict that they will ALL do what I think.

I started reading the MISRA standard and perhaps I'll look into "Effective C++" but, basically, I stay completely away from C++.
 

Offline taydin

  • Frequent Contributor
  • **
  • Posts: 520
  • Country: tr
Re: An 'interesting' thing you can do in C++
« Reply #32 on: September 11, 2018, 04:53:58 pm »
I use C++ often, but only to get the following advantages compared to C: Better type safety, templates, exceptions. Haven't used C++ OOP that much.
Real programmers use machine code!

My hobby projects http://mekatronik.org/forum
 

Offline glarsson

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: se
Re: An 'interesting' thing you can do in C++
« Reply #33 on: September 11, 2018, 05:00:08 pm »
Don't try to predict undefined behavior. It is undefined!

f(a++,a++); is undefined.
a=a++; is undefined.

It doesn't matter if the ++ is on the left or right side of = or the number of ++. What matters is the number of times a variable is modified between sequence points. More than one means undefined behavior.
 
The following users thanked this post: newbrain

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3137
  • Country: ca
Re: An 'interesting' thing you can do in C++
« Reply #34 on: September 11, 2018, 05:14:59 pm »
I know what ++var and var++ do when they are either on the right or left side of the '=' sign.  I wouldn't want to predict what would happen if they are on both sides.  I know what I think they should do but I wouldn't want to bet that any particular compiler will do what I think.  Moreover, I wouldn't want to predict that they will ALL do what I think.

In C, the order is not specified - the evaluation may happen in any order. So, the code like this:

Code: [Select]
x[a++] = a++;
may be executed as either

Code: [Select]
temp1 = a;
a++;
temp2 = a;
a++;
x[temp1] = temp2;

or

Code: [Select]
temp2 = a;
a++;
temp1 = a;
a++;
x[temp1] = temp2;

or even

Code: [Select]
temp1 = a;
temp2 = a;
a++;
a++;
x[temp1] = temp2;

 

Offline glarsson

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: se
Re: An 'interesting' thing you can do in C++
« Reply #35 on: September 11, 2018, 05:34:07 pm »
Or even a[0]=42;.
 
The following users thanked this post: newbrain

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: An 'interesting' thing you can do in C++
« Reply #36 on: September 11, 2018, 05:46:35 pm »
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4527
  • Country: gb
Re: An 'interesting' thing you can do in C++
« Reply #37 on: September 11, 2018, 05:58:42 pm »
Or even a[0]= 42;.
Watt?

What I think he means, is that since it is UNDEFINED, anything can happen.
 
The following users thanked this post: GeorgeOfTheJungle, newbrain

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: An 'interesting' thing you can do in C++
« Reply #38 on: September 11, 2018, 05:59:32 pm »
Or even a[0]= 42;.
Watt?
"undefined" means just that, any result is within the guarantees set by the language, including turning your mother green and causing your computer to explode (OK not, terribly likely but within the permitted behaviour).

 ;)  >:D

Or even a[0]= 42;.
Watt?

What I think he means, is that since it is UNDEFINED, anything can happen.
Yup.
 
The following users thanked this post: GeorgeOfTheJungle, MK14

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2582
  • Country: us
Re: An 'interesting' thing you can do in C++
« Reply #39 on: September 11, 2018, 06:06:53 pm »
Or even a[0]= 42;.
Watt?
"undefined" means just that, any result is within the guarantees set by the language, including turning your mother green and causing your computer to explode (OK not, terribly likely but within the permitted behaviour).

 ;)  >:D

While anything *could* happen, a sensible implementation should give you the expected behavior.
« Last Edit: September 11, 2018, 06:08:24 pm by ajb »
 

Offline glarsson

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: se
Re: An 'interesting' thing you can do in C++
« Reply #40 on: September 11, 2018, 06:18:45 pm »
What is the expected behavior? What is sensible from the point of the compiler will vary depending on compiler, CPU type, optimization level, the source code surrounding the undefined code,..., and the programmer having the expectation.

Please don't argue that undefined behavior do have a defined behavior.
 
The following users thanked this post: hans, ralphrmartin, newbrain, MK14

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2582
  • Country: us
Re: An 'interesting' thing you can do in C++
« Reply #41 on: September 11, 2018, 06:42:00 pm »
What is the expected behavior?
That's explained in the link...
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: An 'interesting' thing you can do in C++
« Reply #42 on: September 11, 2018, 06:52:48 pm »
What is the expected behavior?
That's explained in the link...
See, I think turning your mother green is much better than having daemons fly out of your nose.

Although, possibly, they could then turn your mother green as a parting shot. >:D

 
The following users thanked this post: newbrain

Offline glarsson

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: se
Re: An 'interesting' thing you can do in C++
« Reply #43 on: September 11, 2018, 07:04:46 pm »
That's explained in the link...
No. That discussion is about using sizeof() on an undefined struct. This must be diagnosed by the compiler (error message) and will therefore not have the opportunity to invoke undefined behavior at runtime. So no formatted disks.
 
The following users thanked this post: newbrain

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2582
  • Country: us
Re: An 'interesting' thing you can do in C++
« Reply #44 on: September 11, 2018, 07:09:55 pm »
Perhaps this link will clear things up.
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4527
  • Country: gb
Re: An 'interesting' thing you can do in C++
« Reply #45 on: September 11, 2018, 07:16:19 pm »
Perhaps this link will clear things up.

Sorry!, It hasn't.
My nose is blocked, I'm coughing, the lights have dimmed, strange things are buzzing round and a picture of my mother has just turned green.
 

Offline glarsson

  • Frequent Contributor
  • **
  • Posts: 814
  • Country: se
Re: An 'interesting' thing you can do in C++
« Reply #46 on: September 11, 2018, 07:21:19 pm »
Perhaps you shouldn't run your code on the DeathStation 9000.

http://wikibin.org/articles/deathstation-9000.html
 
The following users thanked this post: MK14

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4527
  • Country: gb
Re: An 'interesting' thing you can do in C++
« Reply #47 on: September 11, 2018, 07:29:45 pm »
Perhaps you shouldn't run your code on the DeathStation 9000.

http://wikibin.org/articles/deathstation-9000.html

That's brilliant!
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26757
  • Country: nl
    • NCT Developments
Re: An 'interesting' thing you can do in C++
« Reply #48 on: September 11, 2018, 08:13:21 pm »
I fully agree. Using these kind of constructs should be banned.
By whom?
By sane people ofcourse  >:D
Unless you want to get stuck maintaining the same code forever you better write your code in a way a complete novice can understand it and not screw it up when making changes.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: TomS_, Mr. Scram

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: An 'interesting' thing you can do in C++
« Reply #49 on: September 11, 2018, 08:45:38 pm »
I fully agree. Using these kind of constructs should be banned.
By whom?
By sane people ofcourse  >:D

Indeed. There's very good advise in Linux kernel coding style document"don’t break the internal parsers of those who will read the code"
 
The following users thanked this post: Ian.M, Mr. Scram


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf