Which C++ feature allows the compiler to insert missing arguments at the call site using specified defaults? Identify the mechanism that fills in omitted parameters automatically.

Difficulty: Easy

Correct Answer: Default arguments

Explanation:


Introduction:
APIs often offer convenient defaults so callers can omit less common parameters. This question asks for the C++ feature that enables such convenience directly in function declarations.


Given Data / Assumptions:

  • A function parameter list may include defaults.
  • Omitted trailing arguments are supplied by the compiler.
  • Language: C++.


Concept / Approach:
Default arguments are specified in the function declaration: void log(std::string msg, int level = 1). When the caller does not provide level, the compiler inserts 1 at the call site during overload resolution. Defaults must be trailing and are bound at the point of declaration that is visible to the call site.


Step-by-Step Solution:
1) Declare defaults in a header or visible declaration.2) Ensure defaults apply to trailing parameters.3) Callers may pass fewer arguments; the compiler fills the rest.4) Implementation should not repeat defaults in multiple declarations to avoid ODR/sync issues.


Verification / Alternative check:
Compile an example where a call omits the final argument and confirm successful binding with the intended value.


Why Other Options Are Wrong:
Call by value/reference/pointer: parameter passing mechanisms, not argument-supply features.


Common Pitfalls:
Placing defaults in both declaration and definition causes redefinition errors. Also avoid ambiguous overloads interacting poorly with defaults.


Final Answer:
Default arguments

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion