Consider this code
class Foo {
private:
Bar bar; //note: no reference
public:
Foo(Bar& b) : bar(b) { }
};
Will Bar get copy-constructed?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That depends on the signatures of
Bar‘s public constructors, either explicitly or implicitly defined.To start with, the C++ standard allows for implicit conversion of references as long as the only difference in the underlying type is that the destination type is at least as cv-qualified than the source type, using the partial ordering defined in this table (C++11, §3.9.3/4):
So, taking that into account as well as §12.8/2:
if Bar has a constructor with any of the following signatures:
then yes,
bwill be copy-constructed intoFoo::bar.EDIT: This was incorrect, I was thinking of
operator=and the details of qualifying as a move-assignment operator.Note that it’s possible to have an eligible constructor that is not a copy constructor:This will work (read: compile), but it is not technically a copy constructor.