Learning Hub Search Ctrl K

Modern C++ › C++14

make_unique and Return Type Deduction

Exception-safe allocation and auto return types.

Since C++14 beginner #make_unique#return-type-deduction#auto#decltype-auto#exception-safety

Explanation

std::make_unique<T>(args...) (C++14) creates a T and returns it in a std::unique_ptr<T>, completing the family started by make_shared in C++11. Benefits over unique_ptr<T>(new T(...)):

  • no raw new in your code, so no chance of leaking before ownership is established;
  • avoids repeating the type name;
  • exception-safe in function-call expressions such as f(make_unique<A>(), make_unique<B>()).

For arrays use std::make_unique<int[]>(n) (value-initialized elements).

Return type deduction for functions: an ordinary function may be declared auto and the compiler deduces the return type from its return statements, with no trailing -> decltype(...) needed. All returns must agree, and the definition must be visible before use. decltype(auto) deduces exactly (preserving references and value category).

Example

#include <iostream>
#include <memory>
#include <string>
#include <vector>

struct Widget {
    std::string name;
    int size;
    Widget(std::string n, int s) : name(std::move(n)), size(s) {}
};

auto square(int x) { return x * x; }                         // returns int

auto makeLabel(const Widget& w) {                            // returns std::string
    return w.name + ":" + std::to_string(w.size);
}

template <typename A, typename B>
auto sum(A a, B b) { return a + b; }                         // no trailing return type

int& pick(std::vector<int>& v) { return v[0]; }

decltype(auto) first(std::vector<int>& v) { return pick(v); }  // keeps the int&

int main() {
    auto w = std::make_unique<Widget>("button", 10);
    std::cout << makeLabel(*w) << " " << square(7) << " " << sum(1, 2.5) << "\n";

    auto arr = std::make_unique<int[]>(5);                   // five zeros
    arr[2] = 7;
    std::cout << arr[2] << arr[0] << "\n";

    std::vector<int> v{1, 2, 3};
    first(v) = 99;                                           // modifies v[0] through the reference
    std::cout << v[0] << "\n";                               // 99

    std::vector<std::unique_ptr<Widget>> widgets;
    widgets.push_back(std::make_unique<Widget>("slider", 3));
    std::cout << widgets.size() << "\n";
    return 0;
}

Common pitfalls

  • auto return type drops references and const: auto f() { return ref; } returns a copy. Use decltype(auto) or auto&.
  • A function using auto return type cannot be used before its definition is seen (no forward declaration with deduction).
  • return statements of different types (return 1; and return 2.0;) fail to deduce.
  • Don’t return decltype(auto) of a local in parentheses: return (x); yields int& to a dead local.
  • make_unique cannot take a custom deleter or brace-init list directly; fall back to unique_ptr<T, D>(new T...).

Legacy vs modern

// C++11
std::unique_ptr<Widget> w(new Widget("button", 10));
template <typename A, typename B>
auto sum(A a, B b) -> decltype(a + b) { return a + b; }
// C++14
auto w = std::make_unique<Widget>("button", 10);
template <typename A, typename B>
auto sum(A a, B b) { return a + b; }

Exercises

1. What is the type returned by auto f(int& r) { return r; }?

int: a copy. Use decltype(auto) or auto& to return a reference.