Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 773
Sprite Avatar asked Oct 27 '25 19:10

Sprite


1 Answers

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
}
like image 83
rustyx Avatar answered Oct 29 '25 08:10

rustyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!