问题标签 [new-expression]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - Direct-init vs. list-init in array new-expression
Basically, why is this valid:
auto p1 = new int[10]{5};
but this is not:
auto p1 = new int[10](5);
And more generally what are the rules for the new-expression initializer?
I found the following:
— If the new-initializer is omitted, the object is default-initialized (8.5). [ Note: If no initialization is performed, the object has an indeterminate value. — end note ] — Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct- initialization.
So is the second case invalid because smth like T((5))
is invalid (direct initialization from expression (5)
)? Or what is the reason?
EDIT: well, my suggestion of (())
thing seems dumb because I see no reason why that should only apply to array new-expression.
c++ - `if constexpr` 的问题与非放置 new
我正在尝试编写一个实用程序来调用new T{...}
或new T(...)
基于是否T
是聚合类型。到目前为止,我所达到的内容如下。请注意,由于这个问题,我使用的是宏而不是函数模板。
我试图在 gcc 7.2.0 上测试它
然后发生以下错误(live)。
编译器谈到了p = new T(__VA_ARGS__);
. ::std::is_aggregate_v<T>
但是当它是真的时,不应该根本不考虑吗?
请注意,使用if constexpr
withplacement new 的类似模式已经奏效。引用自cppref示例。
我想非放置版本有什么特别之处吗?
javascript - new super.constructor 是 JavaScript 中的有效表达式吗?
我想知道表单的表达式new super.SomeProperty
在 JavaScript 中是否有效。
这个问题是在处理跨浏览器行为不一致的代码时出现的,如下面的代码片段中的示例。
这会在 Firefox 和 Edge 中打印一个空对象,但ReferenceError
在 Chrome 和SyntaxError
Safari 中会抛出一个。该错误可以很容易地通过使用括号super.constructor
或使用变量来规避,因此这不是真正的限制,而是对语言本身的好奇问题。我检查了规范,但找不到任何暗示在这种情况下应该抛出错误的东西,所以我很可能遗漏了一些东西。
c++ - 在 C++14 中,在新表达式的维度中使用双精度值是否有效?
在 C++14 中给出以下代码:
clang 在没有诊断的情况下编译它,而另一方面 gcc 产生以下诊断(在 Godbolt 中查看它):
我特别标记了这个 C++14,因为在 C++11 模式下,clang 将其视为格式错误并产生以下诊断信息(在 godbolt 中查看它):
铿锵正确吗?如果是这样,C++14 中发生了什么变化以允许这样做?
c++ - 如何在新表达式中指定构造函数的模板参数?
我从另一段代码中遇到了这个问题,但归结为以下代码段:
这不会令人惊讶地引发以下错误:
这看起来像是以前会遇到一千次的问题,但我在 cppreference 或 SO 上找不到解决方案。
如何在新表达式中指定构造函数的模板参数?
c++ - 为什么 new 表达式可以正确生成指针类型,即使它应该返回 void*?
我们知道,它void*
不包含有关它所指向的数据的实际类型的信息。但是,从 cppreference 开始new
,new[]
我们知道这些运算符返回void*
. 那么,鉴于:
知道new
操作符应该返回void*
,是不是x
就推导出一个类型?int*
void*
再考虑一个例子:
让我们添加一些Type D isplayer :
和测试代码:
代码编译失败,错误提示decltype(x)
是foo*
. 如果我们注释掉 的最后一行main
,我们就会知道,我们的void*
-returning 运算符会因为new called!
被打印而被调用。
c++ - C++ 入门第 5 版:operator new 和 operator delete 重载
有人可以向我解释 C++ 入门第 5 版中的这一段:
术语:new 表达式与 operator new 函数
库函数 operator new 和 operator delete 的名称具有误导性。与其他运算符函数(例如 operator=)不同,这些函数不会重载 new 或 delete 表达式。事实上,我们无法重新定义 new 和 delete 表达式的行为。
new 表达式总是通过调用 operator new 函数来获取内存,然后在该内存中构造一个对象来执行。删除表达式总是通过销毁对象然后调用运算符删除函数来释放对象使用的内存来执行。
通过提供我们自己对 operator new 和 operator delete 函数的定义,我们可以改变内存的分配方式。但是,我们不能改变 new 和 delete 操作符的这个基本含义。
我没有看到operator new
oroperator delete
与任何其他重载运算符(如赋值运算符)之间的区别=
。那么“被误导性命名”是什么意思?我们都知道我们不会重载类似的表达式,fObj + fObj
但我们重载了运算符而不是表达式本身。
事实上,我发现这一段本身就具有误导性。毕竟我们可以“滥用”任何可重载的运算符以及来自哪个运算符new
,delete
那么他在本段中的意思是什么?谢谢!
c++ - 如果我在没有新表达式的情况下直接调用 operator new 并将返回指针类型转换为安全的?
您好,我在 C++ 入门第 5 版的第 19 章。' Operator new
and delete
vs new
anddelete
表达式和位置new
':
AFAIKoperator new
和operator delet
e 分别分配和释放内存,但不在那里构造对象。另一方面,new-expression
调用operator new
分配内存,在该内存地址中构造一个对象,最后返回一个指向新分配和初始化的对象的指针。
所以为了理解起见,我试过这个:
正如您在第一个示例中看到的那样,我直接调用了 operator new,我猜后者只分配内存但不在那里构造对象,最后它返回一个指向 void 的指针,指向新分配的成员。
所以我没有调用构造函数Foo()
。现在为什么调用成员函数可以正常工作?pf->bar()
? 还是这会产生未定义的行为?
第二个例子 ( pf2
) 很简单。
- 如果一个是安全和正确的,发生了什么?我怎么能使用未初始化的对象?谢谢!
c++ - Placement-new vs new-expression
Again with placement new
I've found an example on this forum like this:
But I think here
buf
is a pointer to an allocated and Constructed dynamic array of default-init characters. So the characters in the array are default initialized and have an indeterminate values.I guess using the placement
new
in the second line will constructs objects on the previously constructed array of objects.Why the user didn't call
operator new
on the array allocation rather than usingnew
expression?:After all I think if
buff
is a pointer to a dynamic array of non-default-constructible objects then the code will fail to compile using the new expression rather than using the operator new function.Are my guesses correct?
Here is the link to the original answer:
c# - 如何在 NewExpression 的参数(类型为 MemberExpression)中有效地使用 `?.` 运算符
我想在 NewExpression 中有效地使用空值守卫。我想我最好用例子来解释。
用下面的代码,InstantiateChildClass
设置成true
输出会是下面这样,就完美了。
我想要实现的是下面代码的以下输出,当InstantiateChildClass
设置为false
.
这目前不会发生(当然),因为ChildClass
在src.ChildClass.SomeProp
. 如果不使用表达式来编写它,它会像src.ChildClass?.SomeProp
. 然而,对于表达式,这是不可能的(还没有?),所以NullReferenceExpception
会出现 a。
我玩了几个小时,但我似乎无处可去。除了需要工作代码之外,我真的很想了解如何使用表达式来做到这一点,因为目前它们对我来说仍然有点神奇。我最初通过将编译的方法包装在 try catch 中并在NullReferenceException
. 但是,此代码用于转换数千个对象,并且在使用 try catch '解决方案'时表现不佳。
代码: