提问人:Chris 提问时间:5/20/2022 最后编辑:Chris 更新时间:10/1/2022 访问量:2300
重新安装 Visual Studio 后出现 C7595 错误
C7595 error after reinstalling visual studio
问:
我最近重新安装了 visual studio,并且重新安装并尝试编译我的一些代码,这些代码在那之前已经编译得很好,我遇到了与 std::format 字符串中的常量表达式相关的错误。
我正在使用称为 xorstring 的编译时字符串加密库,以前没有遇到过此错误。下面提供了一个最小的可复制示例:
#include <iostream>
#include <string>
#include <format>
#include <array>
namespace strenc
{
constexpr auto time = __TIME__;
constexpr auto seed = static_cast<int>(time[7]) + static_cast<int>(time[6]) * 10 + static_cast<int>(time[4]) * 60 + static_cast<int>(time[3]) * 600 + static_cast<int>(time[1]) * 3600 + static_cast<int>(time[0]) * 36000;
// 1988, Stephen Park and Keith Miller
// "Random Number Generators: Good Ones Are Hard To Find", considered as "minimal standard"
// Park-Miller 31 bit pseudo-random number generator, implemented with G. Carta's optimisation:
// with 32-bit math and without division
template < int N >
struct RandomGenerator
{
private:
static constexpr unsigned a = 16807; // 7^5
static constexpr unsigned m = 2147483647; // 2^31 - 1
static constexpr unsigned s = RandomGenerator< N - 1 >::value;
static constexpr unsigned lo = a * (s & 0xFFFF); // Multiply lower 16 bits by 16807
static constexpr unsigned hi = a * (s >> 16); // Multiply higher 16 bits by 16807
static constexpr unsigned lo2 = lo + ((hi & 0x7FFF) << 16); // Combine lower 15 bits of hi with lo's upper bits
static constexpr unsigned hi2 = hi >> 15; // Discard lower 15 bits of hi
static constexpr unsigned lo3 = lo2 + hi;
public:
static constexpr unsigned max = m;
static constexpr unsigned value = lo3 > m ? lo3 - m : lo3;
};
template <>
struct RandomGenerator< 0 >
{
static constexpr unsigned value = seed;
};
template < int N, int M >
struct RandomInt
{
static constexpr auto value = RandomGenerator< N + 1 >::value % M;
};
template < int N >
struct RandomChar
{
static const char value = static_cast<char>(1 + RandomInt< N, 0x7F - 1 >::value);
};
template < size_t N, int K >
struct XorWString
{
private:
const wchar_t _key;
std::array< wchar_t, N + 1 > _encrypted;
bool decrypted = false;
constexpr wchar_t enc(wchar_t c) const
{
return c ^ _key;
}
wchar_t dec(wchar_t c) const
{
return c ^ _key;
}
public:
template < size_t... Is >
constexpr __forceinline XorWString(const wchar_t* str, std::index_sequence< Is... >) : _key(RandomChar< K >::value), _encrypted{ enc(str[Is])... }
{
}
__forceinline decltype(auto) decrypt(void)
{
if (!decrypted)
{
for (size_t i = 0; i < N; ++i)
{
_encrypted[i] = dec(_encrypted[i]);
}
_encrypted[N] = '\0';
decrypted = true;
}
return _encrypted.data();
}
};
}
#define xorws( s ) ( strenc::XorWString< sizeof( s ) - 1, __COUNTER__ >( s, std::make_index_sequence< sizeof( s ) - 1>() ).decrypt() )
int main()
{
auto str = std::format(xorws(L"this is a formatted string {}"), 1); // <- error here
}
您应该得到严重性代码描述项目文件行抑制状态 错误 C7595“std::_Basic_format_string<wchar_t,int>::_Basic_format_string”:对即时函数的调用不是常量表达式 APON 尝试运行该程序。
构建日志:
1>------ Build started: Project: test_app, Configuration: Release x64 ------
1>test_app.cpp
1>C:\Users\throw\source\repos\test_app\test_app\test_app.cpp(98,25): error C7595: 'std::_Basic_format_string<wchar_t,int>::_Basic_format_string': call to immediate function is not a constant expression
1>C:\Users\throw\source\repos\test_app\test_app\test_app.cpp(74,142): message : failure was caused by out of range index 30; allowed range is 0 <= index < 30
1>Done building project "test_app.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
编译器信息:
visual studio 2019 latest version
windows sdk version 10.22000
platform toolset v142
language standard /std:c++20
如果这是我的代码的实际错误,我可以做些什么来修复它,为什么我以前没有遇到过这个错误,如果不是,我可以做些什么来补救我的 MSVC 安装。
谢谢!
答:
4赞
Chris
5/21/2022
#1
随着最近对Microsoft STL的更新,现在需要100%的常量值,但它们已经为运行时字符串添加了。我不知道添加了此功能。如果您遇到类似的问题,请尝试改用。std::format
std::vformat
std::vformat
1赞
zeroinfinity
9/19/2022
#2
遇到相同的行为(gcc和clang都可以)。
最小示例:https://gcc.godbolt.org/z/c7ejsM6eh
提交错误 @msvc-dev: https://developercommunity.visualstudio.com/t/C7595-error-on-valid-code/10150938
我的印象 - MSVC consteval 支持需要很多需求。
评论
0赞
Spencer
9/22/2022
嗨,不幸的是,您的问题与 OP 的问题不同。
评论