Home > Backend Development > C++ > body text

When does `auto` in C 11 deduce a value or a reference?

Linda Hamilton
Release: 2024-11-03 15:35:02
Original
898 people have browsed it

When does `auto` in C  11 deduce a value or a reference?

The Subtleties of C 11 "auto" Semantics

In C 11, the auto keyword offers a convenient way to automatically deduce the type of a variable based on its initializer. However, there are certain нюансы to consider regarding whether auto will resolve to a value or a reference.

Understanding the Rule

The rule is straightforward: it depends on how the variable is declared.

<code class="cpp">int i = 5;
auto a1 = i;    // value
auto &a2 = i;  // reference</code>
Copy after login

In the first case, a1 is a value because it was not declared as a reference. In the second case, a2 is a reference because it was explicitly declared as one.

Clarifying the Uncertainties

Let's apply this rule to the examples you provided:

  • auto p = get_foo();: get_foo() returns a reference to a smart pointer. Since p is declared without a reference, it will be a copy, resulting in a reference type object.
  • auto sp = s_foo;: s_foo is a static reference to a smart pointer. When assigning it to sp, the reference will be copied, resulting in a reference type object.
  • for (auto foo: c): foo will be a copy of each iterator in the vector because it is not declared as a reference.

Proof with Template Metaprogramming

The following code demonstrates this behavior using template metaprogramming:

<code class="cpp">#include <typeinfo>
#include <iostream>

template< typename T >
struct A
{
    static void foo(){ std::cout << "value" << std::endl; }
};
template< typename T >
struct A< T&amp;>
{
    static void foo(){ std::cout << "reference" << std::endl; }
};

float& bar()
{
    static float t=5.5;
    return t;
}

int main()
{
    int i = 5;
    int &r = i;

    auto a1 = i;
    auto a2 = r;
    auto a3 = bar();

    A<decltype(i)>::foo();       // value
    A<decltype(r)>::foo();       // reference
    A<decltype(a1)>::foo();      // value
    A<decltype(a2)>::foo();      // value
    A<decltype(bar())>::foo();   // reference
    A<decltype(a3)>::foo();      // value
}</code>
Copy after login

Output:

value
reference
value
value
reference
value
Copy after login

This confirms that the type of auto is determined by its declaration, not by the type of its initializer.

The above is the detailed content of When does `auto` in C 11 deduce a value or a reference?. 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!