I have a code section of the form
auto myObject = someFunc <T> ();
someFunc
returns an object of type T
by value.
Despite setting T& T::operator= ( T const& other )
and T& T::operator= ( T && other )
to private. I get no compiler error.
How do I find out what the expression actually uses?
CodePudding user response:
In
auto myObject = someFunc <T> ();
no operator =
is used. When you have
type_name variable_name = intializer
you are declaring and initializing an object and only constructors are used to initialize an object.
operator =
is only used when you are doing assignment, and that only occurs after you've defined a variables.
In your code depending on a number of conditions you will either see return value optimization (RVO) or named return value optimization (NRVO) meaning you will only see one constructor call or a copy/move operation will happen and you will see two constructor calls happen. One for the object created in someFunc
and the other to copy/move that value into myObject
.
type_name variable_name = intializer; // initialization
variable_name = something; // assignment