C7595 error after reinstalling visual studio

Viewed 679

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!

2 Answers

With the recent update to the Microsoft's STL, std::format now requires 100% constant values, but they have added std::vformat so for runtime strings. I did not know this feature was added. If you have run into similar issues, try using std::vformat instead.

Related