LLVM Tutorial #20: For loops!

preview_player
Показать описание
In this series I walkthrough the LLVM "Kaleidoscope" Tutorial, where you follow step by step to create your first programming language frontend using LLVM as the backend. Last time we finished implementing if statements. This time we continue on to the second half of chapter 5 and implement the parser for the for loop expression.
Рекомендации по теме
Комментарии
Автор

The reason you have to do 2 moves of the unique_ptr is because you have 3 owners of the value in total and 2 of them should lose their ownership through std::move, so only 1 owner is actually holding the value in the end.
The 3 owners are:
- the variable before the constructor is called
- the constructor argument and
- the internal member of the class.

With the first move you transfer ownership from the external variable to the constructor argument. And then you do another move in the constructor to transfer ownership from the constructor argument to the internal member of the class.
Most C++ compilers can optimized these 2 moves and turn them into a single move (or no move at all if the external variable is an rvalue).

zamf