Questions tagged [new-expression]
10 questions
42
votes
2 answers
In C++14 is it valid to use a double in the dimension of a new expression?
In C++14 given the following code:
void foo() {
double d = 5.0;
auto p1 = new int[d];
}
clang compiles this without diagnostic while gcc on the other hand produces the following diagnostic (see it live in godbolt):
error: expression in…

Shafik Yaghmour
- 154,301
- 39
- 440
- 740
8
votes
1 answer
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…

ledonter
- 1,269
- 9
- 27
7
votes
1 answer
How come a new-expression can correctly produce the pointer type, even though it should return void*?
We know that void* holds no information regarding the actual type of the data it points to. However, from cppreference on new and new[] we know that those operators return void*. How come, then, given:
auto x = new int{};
knowing that the new…

Fureeish
- 12,533
- 4
- 32
- 62
7
votes
1 answer
Is new super.constructor a valid expression in JavaScript?
I would like to know if expressions of the form new super.SomeProperty are valid in JavaScript.
This question arose while working with code that behaves inconsistently across browsers like the example in the snippet below.
class Test {
test()…

GOTO 0
- 42,323
- 22
- 125
- 158
3
votes
1 answer
C++ primer 5th edition: operator new and operator delete overloading
can someone explain to me this paragraph from C++ primer 5th edition:
Terminology: new Expression versus operator new Function
The library functions operator new and operator delete are
misleadingly named. Unlike other operator functions, such as…

Itachi Uchiwa
- 3,044
- 12
- 26
2
votes
2 answers
If I call operator new directly without a new expression and cast the return pointer type safe?
Hello I am on chapter 19 from C++ primer 5th edition. 'Operator new and delete vs new and delete expression and placement new':
AFAIK operator new and operator delete allocate and deallocate memory respectively but don't construct an object there.…

Itachi Uchiwa
- 3,044
- 12
- 26
1
vote
5 answers
How to store a variable or object on desired memory location?
Let's suppose I have an object of a class as shown below:
Myclass obj;
Now I want to store this object to my desired memory location. How can I do this? Is it possible or not?
I have created an array class which simply insert data of integer type…

Hassan Ali
- 35
- 8
1
vote
3 answers
How to specify constructor's template arguments inside a new expression?
This issue occurred to me from an other piece of code but it boils down to this snippet:
#include
struct A
{
template
A() : _i{I} {}
int _i;
};
int main()
{
A* ptr = new A; // how to call the constructor with a…

matovitch
- 1,264
- 11
- 26
0
votes
4 answers
Placement-new vs new-expression
Again with placement new I've found an example on this forum like this:
char *buf = new char[sizeof(string)]; // pre-allocated buffer
string *p = new (buf) string("hi"); // placement new
string *q = new string("hi"); // ordinary heap…

Itachi Uchiwa
- 3,044
- 12
- 26
0
votes
2 answers
Issue of `if constexpr` with non-placement new
I'm trying to write a utility that invokes either new T{...} or new T(...) based on whether T is an aggregate type. What I have reached so far is as follows. Note that I'm using a macro instead of a function template because of this issue.
#define…

Lingxi
- 14,579
- 2
- 37
- 93