range-for循环究竟做了什么?

GKx*_*Kxx 0 c++ for-loop const c++11

我正在制作蛇游戏计划.我在类Snake中使用Body的deque代表蛇,当然Body是我定义的结构.以下是代码的一部分:

struct Body {        // one part of snake body
    int x, y, direction;
    Body() : x(0), y(0), direction(UP) { }
    Body(int ix, int iy, int id) : x(ix), y(iy), direction(id) { }
};

class Snake {
protected:
    std::deque<Body> body;
    // other members
public:
    auto begin()->std::deque<Body>::const_iterator const { return body.cbegin(); }
    auto end()->std::deque<Body>::const_iterator const { return body.cend(); }
    // other members
};
Run Code Online (Sandbox Code Playgroud)

在另一个函数construct_random_food中,我需要生成食物并确保它与蛇不一致.这是函数定义:

Food construct_random_food(int gameSize, const Snake& snake) {
    static std::random_device rd;
    static std::uniform_int_distribution<> u(2, gameSize + 1);
    static std::default_random_engine e(rd());
    Food f;
    while (1) {
        f.x = u(e) * 2 - 1;
        f.y = u(e);
        bool coincide = 0;
        for (const auto& bd : snake) // This causes an error.
            if (bd.x == f.x && bd.y == f.y) {
                coincide = 1; break;
            }
        if (!coincide) break;
    }
    return f;
}
Run Code Online (Sandbox Code Playgroud)

基于范围的for循环线引起错误.它说我正在尝试将const Snake转换为Snake&(抛出一个低级别的const).我通过重写这样的行解决了这个问题:

for (const auto& fd : const_cast<Snake&>(snake))
Run Code Online (Sandbox Code Playgroud)

所以我想知道什么是范围 - 做什么和它需要什么.错误是否与类Snake中的begin()函数有关?

Ben*_*ley 6

问题是你beginend函数不是常量.

auto begin()->std::deque<Body>::const_iterator const { return body.cbegin(); }
            // this applies to the return type ^^^^^
Run Code Online (Sandbox Code Playgroud)

您已将const限定符应用于返回类型,而不是调用对象.将const限定符放在尾随返回类型之前.

auto begin() const ->std::deque<Body>::const_iterator { return body.cbegin(); }
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看应在何处放置函数限定符的正确顺序:http://en.cppreference.com/w/cpp/language/function