Abseil C++

An open-source collection of C++ library code designed to augment the C++ standard library

  • Tip of the Week #186: Prefer to Put Functions in the Unnamed Namespace
    https://abseil.io/tips/186

    Originally posted as TotW #186 on November 5, 2020

    By James Dennett and Jason Rennie

    Updated 2020-11-05

    Quicklink: abseil.io/tips/186

    “Everything should be made as simple as possible, but no simpler.” ~ Roger Sessions’s interpretation of Einstein

    When adding a new function, default to making it a non-member function local to the .cc file where it is called. While there are valid reasons to make another choice, consider writing it in an unnamed namespace (also known as an “anonymous namespace”).

    Benefits

    Writing a non-member in an unnamed namespace has benefits both by making functions internal to a .cc file (moving them out of header files) as well as by making them non-members (moving them out of classes).

    Benefits over functions declared in a header file include:

    Making it easy for a (...)