3

Normally the = in a variable declaration is treated by the compiler as move construction. For example:

vector<int> foo = vector<int>(13, 0);

Will call the vector<int>::vector<int>(vector<int>&&) constructor.

How does this work with make_pair? Am I constructing a temporary and moving it if I do:

pair<int, int> foo = make_pair(13, 0);

Obviously the consequence becomes more significant as the types in the pair are something heavier than ints, but the question remains the same. Is a temporary constructed here? If make_pair forces a temporary, I assume I could call:

pair<int, int> foo = pair(13, 0);

Either way, what about when the types can be implicitly converted, but are not identical? For example:

pair<string, int> foo = make_pair<"Hello World!", 13>;

Or:

pair<string, int> foo = pair("Hello World!", 13);

Is a temporary pair<const char*, int> created in this situation?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • "Normal assignment operators" that isn't an assignment operator. – juanchopanza Nov 30 '15 at 12:51
  • 1
    I would not say it is a duplicate. The question is in the title and not the same as the referenced duplicate. – Ely Nov 30 '15 at 13:08
  • @juanchopanza While Copy Elision is what I'm asking about, there isn't an answer there as to whether `make_pair` will be copy elided. Can you please reopen so I can get an answer pertaining to `make_pair`? – Jonathan Mee Nov 30 '15 at 13:10
  • @Elyasin The duplicate answers the question. – juanchopanza Nov 30 '15 at 13:13
  • @JonathanMee Funny, you seem to be asking about move construction and confusing initialization with assignment. Maybe you can clarify your question first. – juanchopanza Nov 30 '15 at 13:16
  • 1
    I think one needs to be able to construct the bridges to understand that the duplicate answers the question(s). You're right though. However, I think here we might benefit from a concrete answer at an example from the STL, i.e. make_pair. – Ely Nov 30 '15 at 13:26
  • @juanchopanza I think you may be pointing out a misconception that I have. I've tried to clarify the question, but if it is a misconception, I might need you to help straighten out my understanding before I can actually clarify the question. – Jonathan Mee Nov 30 '15 at 13:29
  • 1
    First misconception is in my first comment. Second, you say an initialization expression involves a call to the move copy operator, but it doesn't if copy elision kicks in. You seem to be confusing assignment with initialization, and probably move semantics with copy elision. – juanchopanza Nov 30 '15 at 13:35
  • @juanchopanza You say "That isn't an assignment operator", I know that it's normally elided, but I thought elision was up to the implementation, and it would be a legal implementation to compile this as two default constructors and an assignment operator. Is that not the case? – Jonathan Mee Nov 30 '15 at 14:10
  • 1
    It not being an assignment operator has absolutely nothing to do with copies being elided or not. I think you are very confused. Assignment and initialization are different things in C++. And `T t = whatever;` is initialization, not assignment. – juanchopanza Nov 30 '15 at 14:19
  • @juanchopanza I've edited to differentiate between assignment and initialization. Hopefully that meets your standard of excellence such that it can be reopened. – Jonathan Mee Nov 30 '15 at 14:30
  • @juanchopanza I assumed when you said: "Maybe you can clarify your question first" you meant you were gonna reopen? – Jonathan Mee Dec 01 '15 at 14:36
  • @JonathanMee No, I still think it is a duplicate. You're just too confused to see it. – juanchopanza Dec 01 '15 at 17:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96708/discussion-between-jonathan-mee-and-juanchopanza). – Jonathan Mee Dec 01 '15 at 17:23

1 Answers1

0

std::make_pair returns rvalue of pair object, then assignment operator = move the rvalue to lvalue, since std::pair have defined the copy/move assignment operator.

"Assignment to objects of a class is defined by the copy/move assignment operator."

Reference: Cpp11 Standard 5.17 Assignment and compound assignment operators.

Yong Yang
  • 126
  • 6