Skip to end of metadata
Go to start of metadata
Icon

This page is a high level list of general C++ best practices, do's and don'ts, etc.

Don't use std::auto_ptr. Use boost::shared_ptr instead.

There are some nuances to std::auto_ptr that can be problematic if you're not careful. It's even been deprecated in C++11. As a general rule, we just avoid std::auto_ptr to avoid the problems.

See this article for more details.

Watch out for cyclic references when using reference counting smart pointers.

When using reference counting pointers (boost::shared_ptr, Ice Ptr types), avoid cyclic references of objects. These could lead to memory leaks, which (ironically) defeats the purpose of the smart pointers in the first place.

Pass smart pointers as const & instead of by value

When passing smart pointers (boost_shared_ptr, Ice Ptr types, Ice Prx types) as function parameters, pass them as const & instead of pass-by value. This saves on some needless intermediate copies, since the caller should hold their reference to the object for the duration of the call.

But do not store members or local variables as const &. This would fail to increment reference counts, so an object may get deleted before its time.

Avoid volatile

The volatile keyword, by its very definition is compiler specific. Its usage could lead to portability problems and is best avoided.

Do Not Allow Exceptions to "Escape" Destructors

An uncaught exception thrown from a destructor when the stack is unwinding due to a prior exception results in terminate(). If a destructor calls code that may throw, it should surround that code in a try/catch block.

  • No labels