Live data from Hacker News

My Most Important C++ Aha Moments (2006)

artima.com

21–30 of 84 posts

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

#21
post #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…

It might compile, but it won't link.

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

#22

> 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 cou…

Also check out Eli Bendersky's website, he has a cool guide on multiple dispatch.

http://eli.thegreenplace.net/2016/a-polyglots-guide-to-multi...

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

#23
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.

You can just do

    class B;
    class A {
      B* _b;
    };
and it'll be happy.

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

#24
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.

It can easily be done by declaring a prototype of B:

  // file: A.h
  class B;

  class A {
    B* _b;
  };

  // file: B.h
  class B {
    A* _a;
  };

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

#25

> 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 ;)

This is my ignorance speaking, but with the gcc, clang, and msvc all fully supporting C++11, where are the holdups?

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

#26
post #3

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

It's easy to dislike or hate C++ due to its immense complexity, and programmers like to be able to keep simple abstractions in their head so they can concentrate their energies on their problem domain without worrying about language confusion. With C and many other languages, this is possible as they are smaller languages. C++ on the other hand: is there any language with widespread use that is so deep and complex? However, there are undeniable conveniences to C++ (safety, a good library, much more -- especially functional-style programming with lambdas now) that can speed up your work process, and if you can accept that you will never know the entire language and can just use those bits that make things easier, it is possible to have a healthy relationship with the language. And it offers tremendous room to grow as you get more and more comfortable with it. Few languages allow you to just keep diving in deeper and deeper as you desire, while still letting you get to work right away without being a master. Even template metaprogramming alone is gigantic area of ongoing research -- the stuff you can do with it is mind-bending and unavailable in nearly all other languages. Templates are, after all, one of very very few wholly functional, immutable languages out there. And template instantiation is the underlying force behind many of the modern features like lambdas that allow you to access that power without actually knowing templates well. It's a powerful language for which you must accept its character flaws in return for its abilities.

It's doesn't hurt either that the libraries available in C++ are really cool.

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

#27
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.

Yes, but the distractions of C are just as inconvenient, though wholly different. The amount of extra low-level work you must do in a C program considerably adds to the time you need to make it work, time that could be spent thinking about more interesting things. I'd rather reach for a C++ vector and just let it manage the memory for me (or unique_ptr or many other easy tools) while I'm crafting my program logic then spend a nontrivial fraction of my time making sure my memory handling is correct.

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

#29
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.

Check out the Juce library. It's a fantastic lib, primarily intended for audio programming but I've found the GUI tools to be very nice.

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

#30

> 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…

Alternative view: if you must use C++, stick to nothing later than C++2003.

Anyway, declaring a constructor " = delete" isn't the same thing as making it private. If it's private, the code in your class scope can still use it. If you can't trust your class private code to do things correctly, then you're screwed; go join IT and write backup shell scripts. So basically this "= delete" is just another ear growing out of the elbow of C++.

Post reply on HN