考虑下面的代码:
#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.2
和x64 msvc v19.28
。(前往Godbolt查看)