Abseil C++

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

  • Tip of the Week #134: make_unique and private constructors
    https://abseil.io/tips/134

    Originally posted as TotW #134 on Aug 30, 2017

    by Yitzhak Mandelbaum, Google Engineer

    So, you read Tip 126 and are ready to leave new behind. Everything’s going fine until you try to use absl::make_unique() or std::make_unique() (C++14) to construct an object with a private constructor, and it fails to compile. Let’s take a look at a concrete example of this problem to understand what went wrong. Then, we can discuss some solutions.

    Example: Manufacturing Widgets

    You’re defining a class to represent widgets. Each widget has an identifier and those identifiers are subject to certain constraints. To ensure those constraints are always met, you declare the constructor of the Widget class private and provide users with a factory function, Make(), for generating widgets with proper (...)