Skip to Content
Advanced C++ Programming Cookbook
book

Advanced C++ Programming Cookbook

by Dr. Rian Quinn
January 2020
Intermediate to advanced
454 pages
11h 25m
English
Packt Publishing
Content preview from Advanced C++ Programming Cookbook

How it works...

To better understand the extent to which code has to execute so as to allocate a variable on the heap, we will start with the following simple example:

int main(void){    auto ptr = std::make_unique<int>();}

As shown in the preceding example, we allocate an integer using std::unique_ptr(). We use std::unique_ptr() as our starting point, as this is how most C++ Core Guideline code will allocate memory on the heap.

The std::make_unique() function allocates a std::unique_ptr using the following pseudo logic (this is a simplified example as this doesn't show how custom deleters are handled):

namespace std{    template<typename T, typename... ARGS>    auto make_unique(ARGS... args)    { return std::unique_ptr(new T(std::forward<ARGS>(args)...)); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern C++ Programming Cookbook

Modern C++ Programming Cookbook

Marius Bancila

Publisher Resources

ISBN: 9781838559915Supplemental Content