重新安装Visual Studio后的C7595错误
我最近重新安装了Visual Studio,Apon重新安装并试图编译一些我的代码,这些代码已经完全编译了,直到那时,我遇到了与STD ::格式化字符串中常数表达式有关的错误。
我正在使用称为Xorsring的编译时字符串加密库,以前尚未遇到此错误。下面提供了最低可再现的示例:
#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&lt; wchar_t,int&gt; :: _ 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安装。
谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
随着Microsoft STL的最新更新,
std ::格式
现在需要100%常数值,但它们已为运行时字符串添加了std :: vformat
。我不知道添加了此功能。如果您遇到了类似的问题,请尝试使用std :: vformat
而不是。With the recent update to the Microsoft's STL,
std::format
now requires 100% constant values, but they have addedstd::vformat
for runtime strings. I did not know this feature was added. If you have run into similar issues, try usingstd::vformat
instead.满足相同的行为(GCC&amp; clang还可以)。
最小示例: https://gcc.godbolt.org.org/z/c7ejsm6eh
-dev:
我的印象-MSVC Consteval支持希望有很多需要。
Met the same behavior (gcc & clang are ok).
Minimal example: https://gcc.godbolt.org/z/c7ejsM6eh
Filed bug @msvc-dev: https://developercommunity.visualstudio.com/t/C7595-error-on-valid-code/10150938
My impression - MSVC consteval support wants a huge lot to desired.