Understanding Grammar Like u=vector<int>(3) in C++

Asked 1 years ago, Updated 1 years ago, 37 views

C++

vector<int>u;
u=vector<int>(3);

What kind of grammatical structure is it to substitute vector<int>(3) for u?
I understand that (3) after vector<int> represents the number of elements during vector class initialization, but I don't know who vector<int>(3) is. Can you return the value to the right?In which document is the rule written?
Also,

u=*(new vector<int>(3));

What is the difference from ?

c++

2022-09-30 21:43

2 Answers

I don't know who vector<int>(3) is.

It means exactly as you see it.Constructor calls to size_type count out of multiple overloads create std:vector<int> with three elements.

What is the grammatical structure of replacing u with vector<int>(3)?

It does not mean that std::vector also overloads operator=() with operator overload.
Because vector&other, the right side value vector<int>(3) is moved to the left side value u.In addition, the right side value vector<int>(3) is no longer used and is released by the destructor.

u=*(new vector<int>(3));

The same is true that the right side value *(new vector<int>(3)) is moved to the left side value u.However, new vector<int>(3) is only a pointer and is not released.You must explicitly delete.


2022-09-30 21:43

Add only the parts that are not explained

Is there a rule that writing vector<int>(3) returns the object with the right side value?

Yes

Which document is it written in?

The official documentation will be ISO/IEC 14882 C++ language specification.Oira only has a very old ISO/IEC 14882:1998 and its translation, JIS X 3014:2003, but here's a quote from you.

3.10 Left and right values 6
(6) An expression having a temporary object resulting from a cast to a non-reference type shall be a right-hand value (this includes the case of explicit generation of an object using function notation)


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.