重新安装后出现 C7595 错误 visual studio

C7595 error after reinstalling visual studio

我最近重新安装了 visual studio,然后重新安装并尝试编译我的一些代码,这些代码在那时之前已经完全编译好了,我遇到了与 [=29 中的常量表达式相关的错误=] 字符串.

我正在使用称为 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] = '[=11=]';
                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
}

你应该得到 Severity Code Description Project File Line Suppression State 错误 C7595 'std::_Basic_format_string<wchar_t,int>::_Basic_format_string': 对立即数函数的调用不是常量表达式 尝试 运行 程序。

构建日志:

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 安装.

谢谢!

随着最近对 microsft STL 的更新,std::format 现在需要 100% 常量值,但他们为 运行 时间字符串添加了 std::vformat。我不知道添加了此功能。如果您遇到类似问题 运行,请尝试使用 std::vformat。