如何使用新的语法 [英] How to use new syntax

查看:129
本文介绍了如何使用新的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读一些文章,我发现这行是如此隐秘。

  new(new_ptr + i)T :: move(old_ptr [i]));有没有人能解释这种语法的工作原理?

  new(new_ptr + i)T(std: :move(old_ptr [i])); 

这里我们可以简化为:

  new(< destinationLocation>)T(< constructor parameters); 

这是normall C ++ 03,基本上允许你在内存区域动态创建一个对象你已经预先分配(它的一种高级技术,大多数人不会使用(除非他们正在建立自己的容器像对象))。



std :: move ()部分来自C ++ 11,并创建一个特殊的引用类型,允许在T类型中使用move构造函数。

  new T(std :: move(< source Obj>)); 

这基本上说,使用源对象创建一个新的T,并使用move constructor。这意味着源Obj将在移动后保持未定义状态(因此不可用),但它允许从有效的对象创建。



组合两个你placement new with move使用数组中的元素作为源对象的移动构造。


When I am reading some article i found this line is so cryptic.

new (new_ptr + i) T(std::move(old_ptr[i]));

Can somebody explains how this syntax works?

解决方案

Its a placement.

new (new_ptr + i) T(std::move(old_ptr[i]));

Here we can simplify this to:

new (<destinationLocation>) T(<constructor parameters);

This is normall C++03 and basically allows you to create an object dynamically in an area of memory that you have pre-allocated (its an advanced technique that most people will not use (unless they are building their own container like objects)).

The std::move() part is from C++11 and is creating a special reference type that allowes the move constructor to be used in the type T.

new T(std::move(<source Obj>));

This basically says create a new T using the source object and use the move constructor for effeciency. This means the 'source Obj' will be left in an undefined state (thus not usable) after the move but it allows from efficient object creation.

Combining the two you get placement new with move construction using an element from an array as the source object.

这篇关于如何使用新的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆