decltype可以声明一个r值吗?

时间:2012-10-16 05:47:36

标签: c++ visual-c++ c++11 visual-studio-2012 decltype

// Compiled by Visual Studio 2012

struct A
{
    bool operator ==(const A& other) const
    {
        for (decltype(this->n) i = 0; i < n; ++i) // OK
        {}

        return true;
    }

protected:
    size_t n;
};

struct B : public A
{
    bool operator ==(const B& other) const
    {
        for (decltype(this->n) i = 0; i < n; ++i) // error C2105: '++' needs l-value
        {}

        return true;
    }
};

这是VC ++ 2012的错误吗?

1 个答案:

答案 0 :(得分:6)

这似乎是VS2012编译器错误。规范在第7.1.6.2节第4段中非常清楚。实际上,给出的一个例子显示了一个通过const指针a引用的表达式。 decltype(a->x)产生double,而decltype((a->x))产生double const &

所以这是一个错误;编译器认为iconst,因此不能++

相关问题