References in declarations: which statements are correct? An array of references is acceptable. We can create a reference to a reference.

Difficulty: Easy

Correct Answer: Both 1 and 2 are incorrect.

Explanation:


Introduction / Context:
C++ references have deliberate restrictions that simplify usage and avoid certain categories of errors. This question tests familiarity with two such restrictions: arrays of references and references to references.



Given Data / Assumptions:

  • We are discussing direct declarations, not template reference collapsing rules.
  • Standard C++ syntax and type system apply.
  • No compiler extensions are assumed.



Concept / Approach:
(1) Arrays of references are ill-formed because references are not objects; they are aliases and must be bound individually. You can have an array of pointers or an array of std::reference_wrapper objects, but not an array of T&. (2) A 'reference to a reference' is not a distinct type in ordinary declarations; attempts to declare T&& & collapse during template deduction to a reference, but direct 'reference of reference' is ill-formed. Reference collapsing rules apply in templates and type aliases, not as a way to form nested reference types.



Step-by-Step Solution:
1) Try declaring 'int &arr[3];' → compilation error.2) Try declaring 'int & &rr = ...;' → ill-formed in direct code.3) Use alternatives: pointers or std::reference_wrapper for arrays; rely on reference collapsing only in template contexts.4) Conclude both statements are incorrect.



Verification / Alternative check:
Compile minimal examples with common compilers; both constructs fail outside of template deduction scenarios, confirming the rule.



Why Other Options Are Wrong:
Any option asserting 1 or 2 is correct contradicts standard C++ rules.



Common Pitfalls:
Confusing reference collapsing (e.g., T& & → T&) with the notion that references to references are allowed generally. They are not; collapses are a template metaprogramming rule.



Final Answer:
Both 1 and 2 are incorrect.


More Questions from References

Discussion & Comments

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