Initialization and Execution Mechanics
When a function is invoked with a value parameter, the C++ standard abstract machine dictates the following sequence of operations:- Initialization: The parameter is copy-initialized from the caller’s argument.
- For fundamental types (e.g.,
int,double, pointers), the compiler performs standard copy initialization. Depending on the Application Binary Interface (ABI) and the specific type, this may involve passing the value directly in a CPU register rather than allocating memory. - For user-defined types (e.g.,
class,struct) passed as lvalues, the compiler invokes the type’s copy constructor (T::T(const T&)) to initialize the local parameter using the caller’s object.
- For fundamental types (e.g.,
- Destruction: Upon function termination, the local parameter’s lifetime ends. For user-defined types, the destructor (
T::~T()) is invoked on the local instance.
Object Slicing
A critical technical implication of value parameters in C++ is object slicing. If a function parameter is defined as a base class passed by value, and the caller passes a derived class object, the compiler will only invoke the base class’s copy constructor. The resulting local parameter will be a pure base class object. All derived-class member variables are discarded (sliced off), and polymorphic behavior (virtual function resolution) is lost because the dynamic type of the copy is strictly the base type.Move Semantics and Copy Elision
Value parameters interact directly with move semantics and copy elision rules. The initialization behavior changes depending on the value category of the argument provided by the caller:- Lvalues: Passing an lvalue invokes the copy constructor, duplicating the underlying resources.
- Xvalues (Explicit Moves): If the caller passes an xvalue (e.g., an explicitly moved object via
std::move), the compiler invokes the move constructor (T::T(T&&)). The underlying resources are transferred to the value parameter rather than duplicated. - Prvalues (Temporaries): Since C++17, mandatory copy elision (guaranteed copy elision) dictates that passing a prvalue (such as a temporary object) directly initializes the parameter in place. Neither the copy constructor nor the move constructor is invoked, completely bypassing any overhead.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More





