Live data from Hacker News

My Most Important C++ Aha Moments (2006)

artima.com

41–50 of 84 posts

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

#41

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

Interesting, I didn't know about `delete`. Come to think of it, the trick to make something private in order to "remove" it is one of these very few areas where C++ makes you pay for something you don't use, so I'm not surprised that there is now a special keyword (well, a repurposed keyword) to address that.

A declared-private but unimplemented method shouldn't have any runtime cost. =delete is just a syntactic convenience.

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

#42
post #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.

I'm sorry, but C++ does not exactly have a culture of secure, maintainable and understandable software.

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

#43
post #21
post #18

Earlier quoted context omitted.

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.

It will likely link, since both clang and gcc will perform RVO on bar(), so the copy constructor won't be called.

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

#44
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? H…

> safety

Um, no. C++ can be safer than C (which is an incredibly low bar) but only if you carefully constrain yourself to certain subsets of the language, which is why the web is chock-a-block with byzantine "C++ coding standards" documents describing all the rules you have to follow in order to avoid shooting yourself in the foot.

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

#45
post #44

Earlier quoted context omitted.

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

> safety Um, no. C++ can be safer than C (which is an incredibly low bar) but only if you carefully constrain yourself to certain subsets of the language, which is why the web is chock-a-block with byzantine "C++ coding standards" documents describing all the rules you have to follow in order to avoid shooting yourself in the foot.

Indeed that not only you have to follow, everyone else on the project has to follow, as well as every newcomer and every piece of third-party code that is brought in.

"Avoid the parts you don't like" works best for one programmer working alone.

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

#46
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? H…

> if you can accept that you will never know the entire language

As a developer responsible for understanding what everyone else is doing on the same project, no I cannot accept that.

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

#47
post #44

Earlier quoted context omitted.

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

> safety Um, no. C++ can be safer than C (which is an incredibly low bar) but only if you carefully constrain yourself to certain subsets of the language, which is why the web is chock-a-block with byzantine "C++ coding standards" documents describing all the rules you have to follow in order to avoid shooting yourself in the foot.

>only if you carefully constrain yourself to certain subsets of the language

I'm curious which subsets are included in this?

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

#48

Earlier quoted context omitted.

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

Icc

ICC supports delete! Auto type deduction and uniform initialization syntax are another story...

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

#49
post #44

Earlier quoted context omitted.

> safety Um, no. C++ can be safer than C (which is an incredibly low bar) but only if you carefully constrain yourself to certain subsets of the language, which is why the web is chock-a-block with byzantine "C++ coding standards" documents describing all the rules you have to follow in order to avoid shooting yourself in the foot.

Indeed that not only you have to follow, everyone else on the project has to follow, as well as every newcomer and every piece of third-party code that is brought in. "Avoid the parts you don't like" works best for one programmer working alone.

I had an instructor once that insisted on having a single exit point for all loops. "Never use break or continue or return from a loop." The argument was that understanding control flow is more complex when you don't have to deal with these things.

However, those things exist in the language. You can't just pretend nobody is going to use them, because there might be very good reasons to use them, especially in an imperative context.

(Though I suppose if you're grading 100 projects, such a rule might make it easier to get the code structured the same everywhere.)

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

#50
post #19

Earlier quoted context omitted.

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.

I'm sorry, but C++ does not exactly have a culture of secure, maintainable and understandable software.

Yes, it does.

The majority of C++ programmers make use of:

- RAII

- String classes instead of char* überall

- Vector classes instead of char* überall

- new and delete instead of malloc()/free() and casts

- References for out parameters instead of pointers

- Real enums, specially since C++11, instead of #define

- Smart pointers for memory management

- Type based programming to reduce errors

- Minimize the use of the pre-processor to #include and macros, only when they cannot be expressed via templates, const, constexpr and inline.

Most of the unsafe C++ code is written by the "C with C++" sub-community that are mostly C refugees forced to use a C++ compiler.

Also while the C++ community is pushing for safety with activities like the C++ Core Guidelines and reducing the amount of UB in the standard, the C standard community doesn't care at all.

The only thing that the C community has done regarding security was the Annex K in C99, which was proven so good that it became an optional feature in C11.

Post reply on HN