重新安装Visual Studio后的C7595错误

发布于 2025-01-31 05:12:37 字数 4462 浏览 1 评论 0 原文

我最近重新安装了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安装。

谢谢你!

I recently reinstalled visual studio, and apon reinstalling and attempting to compile some code of mine that had compiled completely fine up until that point, I was met with an error related to constant expressions inside of std::format strings.

I am using a compile time string encryption library known as xorstring and have not previously encountered this error. A minimum reproduceable example is provided below:

#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
}

You should get Severity Code Description Project File Line Suppression State
Error C7595 'std::_Basic_format_string<wchar_t,int>::_Basic_format_string': call to immediate function is not a constant expression
apon trying to run the program.

build log:

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 ==========

compiler information:

visual studio 2019 latest version
windows sdk version 10.22000
platform toolset v142
language standard /std:c++20

if this is an actual error with my code, what can I do to fix it, and why wasn't I encountering this error before, and if it isn't, what can I do to remedy my MSVC install.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2025-02-07 05:12:37

随着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 added std::vformat for runtime strings. I did not know this feature was added. If you have run into similar issues, try using std::vformat instead.

甚是思念 2025-02-07 05:12:37

满足相同的行为(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文