c++ - decltype 可以声明一个右值吗?

标签 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 的错误吗?

最佳答案

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

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

关于c++ - decltype 可以声明一个右值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12908297/

相关文章:

c++ - 将 float 转换为整数 : rounding in complex cases (ceil() and floor() not usable)

c++ - sin_addr.S_un如何转换为char *?

c++ - 如何在 Windows 上用 C++ 重命名打印机?

c++ - 使用 C++11 随机库生成随机数

c++ - unique_ptr::deleter_type::pointer 有什么用?

c++ - 在没有事件异常的情况下终止调用(多线程合并排序)

c++ - 无法从 char 三重指针获取或打印 wstring 元素

c++ - 如何在ATL/WTL对话框上注册自定义控件?

c++ - 什么是 do(keyword) c++?

c++ - 尽管 try-catch 异常仍泄漏到系统