Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of llvm::make_unique?

Tags:

c++

llvm

In llvm's compiler implementation tutorial (e.g. here) llvm::make_unique is used. What is the reason they aren't using std::make_unique? I wasn't able to find any clear documentation on this.

like image 486
Saraph Avatar asked Mar 09 '16 19:03

Saraph


People also ask

What is make_unique?

You can use make_unique to create a unique_ptr to an array, but you cannot use make_unique to initialize the array elements. C++ Copy. // Create a unique_ptr to an array of 5 integers. auto p = make_unique<int[]>(5); // Initialize the array. for (int i = 0; i < 5; ++i) { p[i] = i; wcout << p[i] << endl; }

When was make_unique introduced?

The std::make_unique function was introduced in the C++14 standard. Make sure to compile with the -std=c++14 flag to be able to use this function. The object gets destroyed once p goes out of scope.


1 Answers

TL;DR;

LLVM is written using C++11 conforming code while std::make_unique is a C++14 feature. So if they want make_unique they need to implement it.

Details

If we go to the LLVM Coding Standards the C++ Standard Versions section says:

LLVM, Clang, and LLD are currently written using C++11 conforming code, although we restrict ourselves to features which are available in the major toolchains supported as host compilers. The LLDB project is even more aggressive in the set of host compilers supported and thus uses still more features. Regardless of the supported features, code is expected to (when reasonable) be standard, portable, and modern C++11 code. We avoid unnecessary vendor-specific extensions, etc.

We can see from cppreference that std::make_unique is a C++14 feature. If they want to use make_unique then they can't use the std version.

We can see from a recent llvm-dev discussion that moving to C++14 is still an open subject.

like image 183
Shafik Yaghmour Avatar answered Oct 05 '22 23:10

Shafik Yaghmour