【问题标题】:Can decltype declare an r-value?decltype 可以声明一个右值吗?
【发布时间】:2012-10-16 05:47:35
【问题描述】:
// 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 的错误吗?

【问题讨论】:

  • 类型不是右值或左值;类型是类型。左值/右值分类适用于表达式
  • 作为参考,它在 gcc 4.6.3 下编译,带有 C++0x 标志。我认为这是正确的,考虑到你的两个循环是相同的。
  • B::operator== 中 i 的类型推导为 const int,看起来像一个 VC 错误。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 2013-03-11
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2019-05-06
    • 2017-10-09
    • 2016-03-27
    相关资源
    最近更新 更多