Sprite
Sprite

Reputation: 3763

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

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)

Upvotes: 4

Views: 940

Answers (1)

rustyx
rustyx

Reputation: 85452

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
}

Upvotes: 5

Related Questions