Why NRV Optimization require copy constructor ?

D

Dhirendra Singh

In Stanely B. Lippman book "Inside the C++ object model" it is
mentioned that NRV optimization require explicit definition of copy
constructor. Without copy constructor it is turned off.
I do not understand why it require copy constructor ?
I searched the web but do not find any concrete answer.
Any help on this topic is appreciated.
 
B

Bo Persson

Dhirendra said:
In Stanely B. Lippman book "Inside the C++ object model" it is
mentioned that NRV optimization require explicit definition of copy
constructor. Without copy constructor it is turned off.
I do not understand why it require copy constructor ?
I searched the web but do not find any concrete answer.
Any help on this topic is appreciated.

It doesn't require an explicit copy constructor, but an *accessible*
copy constructor. Making it private would disable returning the value,
even if the copy constructor isn't actually called.


Bo Persson
 
A

Abhishek Padmanabh

In Stanely B. Lippman book "Inside the C++ object model" it is
mentioned that NRV optimization require explicit definition of copy
constructor. Without copy constructor it is turned off.
I do not understand why it require copy constructor ?
I searched the web but do not find any concrete answer.
Any help on this topic is appreciated.

The standards does mention about the copy construction optimization
but it doesn't mention about it be explicitly defined in code
(explicitly here is not related to the keyword 'explicit' or even that
is not required by the standards). So, I don't know what that refers
to, I don't have the book.
 
A

Abhishek Padmanabh

It doesn't require an explicit copy constructor, but an *accessible*
copy constructor. Making it private would disable returning the value,
even if the copy constructor isn't actually called.

Making it private would be pointless. I don't think that would have
been the intent of the author. Because once the copy constructor is
not accessible, you wouldn't even be able to copy-construct or return
by value. So, copy constructor being accessible is kind of like
stating the obvious.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top