基于范围的for循环中&和&&之间有什么区别?

LHL*_*ini 11 c++ for-loop reference rvalue-reference

我想知道在这个代码中for (auto& i : v),for (auto&& i : v)在基于范围的for循环之间有什么区别:

#include <iostream>
#include <vector>

int main() 
{
    std::vector<int> v = {0, 1, 2, 3, 4, 5};

    std::cout << "Initial values: ";

    for (auto i : v)    // Prints the initial values
        std::cout << i << ' ';
    std::cout << '\n';

    for (auto i : v)    // Doesn't modify v because i is a copy of each value
        std::cout << ++i << ' ';
    std::cout << '\n';

    for (auto& i : v)   // Modifies v because i is a reference
        std::cout << ++i << ' ';
    std::cout << '\n';

    for (auto&& i : v)  // Modifies v because i is a rvalue reference (Am I right?)
        std::cout << ++i << ' ';
    std::cout << '\n';

    for (const auto &i : v) // Wouldn't compile without the /**/ because i is const
        std::cout << /*++*/i << ' ';
    std::cout << '\n';

}
Run Code Online (Sandbox Code Playgroud)

输出:

初始值:0 1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6
2 3 4 5 6 7
2 3 4 5 6 7

两者似乎都在这里做同样的事情,但我想知道这段代码for (auto& i : v)和之间的区别for (auto&& i : v).

LHL*_*ini 7

在我提出这个问题七年后,我觉得有资格提供一个更完整的答案。

首先我要说的是,我当时选择的代码对于问题的目的来说并不理想。这是因为对于示例来说&和之间没有区别。&&

事情是这样的:两者

std::vector<int> v = {0, 1, 2, 3, 4, 5};

for (auto& i : v)
{
    std::cout << ++i << ' ';
}

std::cout << '\n';
Run Code Online (Sandbox Code Playgroud)

std::vector<int> v = {0, 1, 2, 3, 4, 5};

for (auto&& i : v)
{
    std::cout << ++i << ' ';
}

std::cout << '\n';
Run Code Online (Sandbox Code Playgroud)

是等价的。

这是证明:

#include <vector>

std::vector<int> v;

void f()
{
    for (auto& i : v)
    {
        static_assert(std::is_same<decltype(i), int&>::value);
    }

    for (auto&& i : v)
    {
        static_assert(std::is_same<decltype(i), int&>::value);
    }
}
Run Code Online (Sandbox Code Playgroud)

但为什么?

就像 David G 在评论中所说,由于引用折叠,对左值引用的右值引用变成了左值引用,例如

#include <type_traits>
using T1 = int&;
using T2 = T1&&;
static_assert(std::is_same<T1, T2>::value);
Run Code Online (Sandbox Code Playgroud)

但请注意,这是不同的:

for (int&& i : v)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

并且会失败,因为右值引用无法绑定到左值。引用折叠不适用于这种情况,因为没有类型推导。

TLDR:对于标准容器,基于范围的 for 循环之间&的区别是:&&

  • value_type&已验证
  • value_type&&无效
  • auto&auto&&等于value_type&

现在让我们尝试相反的操作:返回右值的可迭代对象。

#include <iostream>

struct Generated
{
    int operator*() const
    {
        return i;
    }

    Generated& operator++()
    {
        ++i;
        return *this;
    }

    bool operator!=(const Generated& x) const
    {
        return i != x.i;
    }

    int i;
};

struct Generator
{
    Generated begin() const { return { 0 }; }
    Generated end() const { return { 6 }; }
};

int main()
{
    Generator g;

    for (const auto& i : g)
    {
        std::cout << /*++*/i << ' ';
    }
    std::cout << '\n';

    for (auto&& i : g)
    {
        std::cout << ++i << ' ';
    }
    std::cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)

在这里,auto&不起作用,因为您无法将非常量左值绑定到右值。

现在我们实际上有const int&int&&

Generator g;

for (const auto& i : g)
{
    static_assert(std::is_same<decltype(i), const int&>::value);        
}

for (auto&& i : g)
{
    static_assert(std::is_same<decltype(i), int&&>::value); 
}
Run Code Online (Sandbox Code Playgroud)