Home > Backend Development > C++ > body text

When using \'auto\' in C 11, how does it determine whether a variable is a reference or a value?

Susan Sarandon
Release: 2024-11-04 11:24:02
Original
1000 people have browsed it

When using

C 11 "auto" Type Deduction: Resolving Reference vs. Value

In C 11, the "auto" keyword provides a convenient way to automatically deduce the type of a variable. However, when using "auto," it's essential to understand the rules that determine whether it resolves to a value or a reference.

Type Deduction Rules for "auto":

The fundamental rule is that "auto" interprets the declaration of the variable itself, not the type it represents. Therefore, the following example clearly demonstrates that "auto" resolves to a value:

<code class="cpp">auto i = v.begin(); // Copy, as begin() returns an iterator by value</code>
Copy after login

However, in more complex scenarios, the distinction can be less apparent. Consider the following examples:

  • Case 1: Reference or Copy?
<code class="cpp">const std::shared_ptr<Foo>&amp; get_foo();
auto p = get_foo(); // Copy or reference?</code>
Copy after login

In this case, "auto" deduces the type from the return type of get_foo() function, which is a reference to a std::shared_ptr. Since the declaration of p uses a single ampersand (&), it resolves to a copy, not a reference.

  • Case 2: Static Variable - Copy or Reference?
<code class="cpp">static std::shared_ptr<Foo> s_foo;
auto sp = s_foo; // Copy or reference?</code>
Copy after login

Here, "auto" deduces the type from the declaration of s_foo, which is a static std::shared_ptr. Since there is no ampersand in the declaration of sp, it again resolves to a copy.

  • Case 3: Looping Over a Container - Copy for Each Iteration?
<code class="cpp">std::vector<std::shared_ptr<Foo>> c;
for (auto foo: c) { // Copy for every loop iteration?</code>
Copy after login

In this case, "auto" deduces the type from the iterator type of the vector. The iterator returns std::shared_ptr, which is a reference type. However, since the declaration of foo uses an ampersand (&), it resolves to the dereferenced value, which is a copy of the std::shared_ptr for each loop iteration.

Conclusion:

The rule for "auto" type deduction is straightforward: it follows the declaration of the variable itself. To resolve to a reference, use an ampersand in the declaration (auto &ref = ...). Otherwise, "auto" will deduce a value type.

The above is the detailed content of When using \'auto\' in C 11, how does it determine whether a variable is a reference or a value?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!