【问题标题】:Different behavior of consteval in GCC and MSVC (not work)GCC 和 MSVC 中 consteval 的不同行为(不起作用)
【发布时间】:2021-03-31 09:54:01
【问题描述】:

考虑以下代码:

#include <cstdint>
#include <vector>

class ClassT
{
public:
    consteval static size_t GetSize()
    {
        return sizeof(int);
    }

    void Resize()
    {
        _Data.resize(GetSize(), 0);
    }

    std::vector<uint8_t> _Data;
};

int main()
{
    ClassT Object;
    Object.Resize();

    return 0;
}

GCC编译成功,但是MSVC报错:

error C7595: 'ClassT::GetSize': call to immediate function is not a constant expression

我错过了什么吗?还是真的是 MSVC 的错误?

编译器版本:x86-64 gcc 10.2x64 msvc v19.28。 (Link to godbolt)

【问题讨论】:

  • 这绝对是一个 MSVC 错误
  • Reduced。这肯定是一个 MSVC 错误。
  • @Barry 和 AndyG。感谢您的评论,我会将错误报告给 MSVC 开发人员。
  • 他们可能已经知道愚蠢的 consteval 是在 19.27 -> 19.28
  • @ÖöTiib 是的,我发现了一份关于此错误的现有报告 (Link)。下次我会在提问之前更仔细地搜索。

标签: c++ gcc visual-c++ language-lawyer c++20


【解决方案1】:

这看起来像是一个 MSVC 错误。它甚至可能与现有的相同 - #1224555

小例子:

consteval int test() { return 0; }

int main() {
    return test(); // error C7595: 'test': call to immediate function is not a constant expression
}

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多