Different behavior of consteval in GCC and MSVC (not work)
Asked Answered
B

1

4

Consider the following code:

#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 compiles it successfully, but MSVC gives the following error:

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

Am I missing something? Or is it really an MSVC bug?

Compilers version: x86-64 gcc 10.2 and x64 msvc v19.28. (Link to godbolt)

Brownlee answered 21/12, 2020 at 16:6 Comment(5)
This is definitely a MSVC bugRoentgenograph
Reduced. This is surely an MSVC bug.Middleoftheroader
@Middleoftheroader and AndyG. Thanks for the comment, I will report the bug to the MSVC developers.Brownlee
They probably know already as the inane consteval was added 19.27 -> 19.28Meier
@ÖöTiib Yes, I found an existing report about this bug (Link). Next time I will search more carefully before asking questions.Brownlee
B
5

This looks like an MSVC bug. It might even be the same as this existing one- #1224555.

Minimal example:

consteval int test() { return 0; }

int main() {
    return test(); // error C7595: 'test': call to immediate function is not a constant expression
}
Bailiwick answered 21/12, 2020 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.