Live data from Hacker News

My Most Important C++ Aha Moments (2006)

artima.com

11–20 of 84 posts

Re: My Most Important C++ Aha Moments (2006)

#11
> Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Yes, but that's not the real point. You can do that in other ways. Creating a new std::algorithm does that, for example.

The real purpose is letting one function be polymorphic in multiple directions. This is called multiple dispatch. Some languages support this as a built-in feature, but C++ doesn't, so a couple different design patterns were invented to fill in this gap. The Visitor pattern is the most popular and is probably the easiest to work with.

https://en.wikipedia.org/wiki/Multiple_dispatch

That is, it lets you do this:

    polymorphicOnThis->doSomething(alsoPolymorphicOnThis);
That is, for each permutation of the two types (polymorphicOnThis and alsoPolymorphicOnThis), you can define new specific behavior.

Re: My Most Important C++ Aha Moments (2006)

#12
post #6
post #3

I love C and Objective-C. I can't stand C++. Is that common among programmers?

What has always bugged me about C++, but maybe I am overlooking some reason why this generally cannot work, is that it does not resolve circular dependencies such as this one: // file: A.h class A { B* _b; }; // file: B.h class B { A* _a; }; I mean a pointer has a fixed size, so couldn't the compiler just leave some sort of type placeholder in class A until class B is eventually defined.

Structure layout isn't the hard part, it's having the rest of the language cope with the ambiguity of everything.

  // Things.h
  namespace Foo { class A { C* c; }; }
  namespace Bar { class B { C* c; }; }
Are A::c and B::c the same type?

  // TU1.cpp
  class C {};
Were you right?

  // TU2.cpp
  namespace Foo { class C {}; }
  namespace Bar { class C {}; }
What about now?

  // TU3.cpp
  class C {};
  namespace Foo { class C {}; }
  namespace Bar { class C {}; }
This may get you shot, but is legal. What about now?

Ahh - all three are in the same program. Just pretend they #included , , and . Having A::c's type be different in different contexts is a violation of the "One Definition Rule", for which the punishment is undefined behavior.

Although you can still recreate the above scenarios by just #including after and company, but you're a bit more likely to have Things.h #include what it needs to clarify the situation. Hopefully. So maybe we can get away with it!

  void foo() { D * d; }
Is that a pointer definition or invoking operator* against two globals?

  void bar() { E  e; }
Now you're just fucking with me. Is that some comparison operators or a template?

  void baz() { F  f; }
That didn't clarify anything. Stop it.

  void he_comes() { G  g(); }
What do you mean I just declared a function? Stop it!

  v̹̊̈́̇ͦ̌ͭ͂ͅo̅̉̀ͪ̚ī̵̠̘̋d̸͚̪̝̹͙͆͛̿ ͉̜̳ͪͬ́ͣ̌ẑ̸̹̹̩̩a̡̰ͣͭ̄͒ͫ̇̚l̛̹̫͓̣͖͈̐ͬg̢͕̘͙o̪͆̏ͥ͟(̖̭̟̱̰ͩ)̘̬͉̺̉̓ ̦͆́̚{ ̶̪͙̹͈ͮͧ̋ͪͤͅĜ͔̙̉͢ ̝̯̼͍̘̦͖ͭ̈́̎͘ ̧̘̻̭͉̺g̟͓̑ͦ̌̃(̪ͬͪͤ̎̉͘ȉ̮̥͖́n̸̝̪̗̯͇̓̈́̀̔̄̍́t͓̟͍ͨͪ(̰͈̻͎̣͉̀̿̈͑)̝͚̆̃̇)̠̃;̺͎̙̰̈́̏̚͘ ̶̜͍̠̼ͬ}̡̤̰̮̳͈̌͛ͤ̈́
A function accepting a function pointer? Stooooop!

Re: My Most Important C++ Aha Moments (2006)

#13
post #9
post #3

I love C and Objective-C. I can't stand C++. Is that common among programmers?

C programmer here; I don't like C++ much because every library tends to reinvent the whole stack (or maybe that's just my impression from using MFC and Qt, which are notorious in that regard). RAII and scope based construction/destruction is actually quite nice.

> every library tends to reinvent the whole stack

This is a problem with vocabulary types. C tends to do a good job of providing them. C++ tends not to, though there has been some progress in the standard lately in defining classes like unique_ptr and string_view.

C tends to fall down by providing raw memory (an int and a char) to represent fairly subtle relationships (the int represents the size of the string pointed to by the char... what happens when the int is negative?). So C libraries don't 'reinvent the whole stack', but this sort of bookkeeping logic tends to cause bugs (at least security bugs).

Re: My Most Important C++ Aha Moments (2006)

#14
> Realizing that C++’s “special” member functions may be declared private

Don't do this anymore! Just delete them:

    MyClass& operator=(const MyClass&) = delete;
Private-really-means-deleted was a cool and very useful trick before C++11. So useful, that it was a shame to require secret knowledge and aha moments to use. So they just made it a normal feature of the language. There's also a way to explicitly use the default implementation:

    MyClass& operator=(const MyClass&) = default;

Re: My Most Important C++ Aha Moments (2006)

#15

> Realizing that C++’s “special” member functions may be declared private Don't do this anymore! Just delete them: MyClass& operator=(const MyClass&) = delete; Private-really-means-deleted was a cool and very useful trick before C++11. So useful, that it was a shame to require secret knowledge and aha moments to use. So they just made it a normal feature of the language. There's also a way to explicitly use the defau…

> Private-really-means-deleted was a cool and very useful trick before C++11

Rather, until every compiler you need to target supported this specific C++11 feature. In other words, it's still a cool and very useful trick ;)

Re: My Most Important C++ Aha Moments (2006)

#16
post #10
post #3

I love C and Objective-C. I can't stand C++. Is that common among programmers?

The more I learn C++, the more I can't stand it. Its ever-increasing complexity keeps being a distraction from the actual problem you're trying to solve.

I've reached peak loathing of C++ at this point. I can see why people would want to lock up the recursive template metaprogramming double barrled automatic shotgun.

...but I get confused by the people who do that, and then turn around to embrace C, the circus where people juggling the chainsaws of "faking templates with macros", "terrible build times", "yet more security vulnerabilities resulting from undefined behavior", "no C-standard defined threading / async support until C1X", and more.

It's common, but I don't get it.

Re: My Most Important C++ Aha Moments (2006)

#17
post #9
post #3

I love C and Objective-C. I can't stand C++. Is that common among programmers?

C programmer here; I don't like C++ much because every library tends to reinvent the whole stack (or maybe that's just my impression from using MFC and Qt, which are notorious in that regard). RAII and scope based construction/destruction is actually quite nice.

At least C++ has a stack to re-invent. With C you're missing Vector and Map before you even start.

Re: My Most Important C++ Aha Moments (2006)

#18

> Realizing that C++’s “special” member functions may be declared private Don't do this anymore! Just delete them: MyClass& operator=(const MyClass&) = delete; Private-really-means-deleted was a cool and very useful trick before C++11. So useful, that it was a shame to require secret knowledge and aha moments to use. So they just made it a normal feature of the language. There's also a way to explicitly use the defau…

There is an obscure pitfall of making a class noncopyable by making the copy constructor private.

Consider the following class:

  class X { 
   private:
     X& (const X&); // not defined
   public:
     X bar() {
        X x = ......;
        return x;
     }
   };
Due to programmer error, an instance of X is returned by value despite the class being noncopyable. The copy constructor being private doesn't help as bar() is a method of X. This will compile and link, despite the copy constructor being undefined. I will leave the reason for this as an exercise for the reader.

Re: My Most Important C++ Aha Moments (2006)

#19
post #3

I love C and Objective-C. I can't stand C++. Is that common among programmers?

I love C++ except the copy-paste parts from C and the toolchain compromises used to sell it to C developers.

Dislike Objective-C verbosity and C roots, although I kind of like its Smalltalk influence.

Really strongly dislike C and the culture of insecure software it has brought into our industry.

Re: My Most Important C++ Aha Moments (2006)

#20
post #10

Earlier quoted context omitted.

The more I learn C++, the more I can't stand it. Its ever-increasing complexity keeps being a distraction from the actual problem you're trying to solve.

I've reached peak loathing of C++ at this point. I can see why people would want to lock up the recursive template metaprogramming double barrled automatic shotgun. ...but I get confused by the people who do that, and then turn around to embrace C, the circus where people juggling the chainsaws of "faking templates with macros", "terrible build times", "yet more security vulnerabilities resulting from undefined behav…

Well, people like C because it contains less language features and therefore is simpler. The error is thinking that simple and easy are the same thing. C is fiendishly difficult to write correctly.
Post reply on HN