Sunday, December 27, 2015

Preferring This Modern, Newfangled Keyword

Today was mainly a study day due to my busy schedule today. Not busy enough to keep from learning!

Step 1: auto may produce undesired types in the case of proxy classes. Explicitly cast that type when declaring auto variables to prevent this!

Step 2: {} can produce different construction behavior when working with numeric vectors ({10, 20}, what does it make?) and has preference for initializer_list. However, it prevents some nasty things like the parse that makes default constructors look like normal functions.

Step 3: Prefer nullptr to 0 and NULL. One is a pointer (or all types) type, and the others are integral types. This can turn into some nasty overloading when trying to call functions on each different null type, so stick to nullptr.

Step 4: Prefer alias declarations to typedefs. Using blah = tends to be a little better than putting typedefs in structs, along with reducing dependent types while programming.

Step 5: Prefer enum class to enum; scoped enums help strongly type enumerated values and prevents comparing a Color to a number.

Step 6: Prefer deleted functions to private,  undefined functions. Some member or friend might try.

Step 7: Declare overriding functions with override, so the compiler can check whether the function properly overrides something.

Step 8: Prefer noexcept for functions like move operations and swap that do not emit exceptions; this provides  more efficient code that helps to optimize the stack during possible preparation for exceptions.

No comments:

Post a Comment