问题描述
考虑以下代码:
#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
。 (Link to godbolt)
解决方法
这看起来像是一个 MSVC 错误。它甚至可能与现有的相同 - #1224555。
最小示例:
consteval int test() { return 0; }
int main() {
return test(); // error C7595: 'test': call to immediate function is not a constant expression
}