使用 fmt::format 的代码在 Visual Studio 2022 更新 17.4 后出现了问题。

7
我在广泛使用fmtlib中的fmt::format。在将Visual Studio 2022从17.3.5更新到17.4.0之后,我在发布版本构建中遇到了以下代码的问题:
const std::string s1 = format("{}.{}", "abc", "test" );

在调试版本中,s1和s1.c_str()都是"abc.test"。但在使用fmtlib的发布版本中,只有s1是"abc.test",而s1.c_str()返回的是"abc.test"后面跟着一些垃圾字符(例如"abc.testð_àô")。
我编写了一个小的测试程序:
#include <iostream>
#include <string>
#ifdef USE_STD_FORMAT
#include <format>
using std::format;
#else
#include <fmt/format.h>
using fmt::format;
#endif

void check(const std::string& s, const std::string& expected)
{
    if (s != expected)
        std::cout << "FAILED I: '" << s << "' != '" << expected << "'\n";
    else if (strcmp(s.c_str(), expected.c_str()) != 0)
        std::cout << "FAILED II: '" << s.c_str() << "' != '" << expected.c_str() << "'\n";
}

void test(const char* p1, const char* p2)
{
    const std::string s1 = format("{}.{}", p1, p2);
    const std::string s2 = std::string(p1).append(".").append(p2);

    check(s1, s2.c_str());
}

int main()
{
    std::cout << "_MSC_FULL_VER: " << _MSC_FULL_VER << "\n";
#if USE_STD_FORMAT
    std::cout << "using std::format\n";
#else
    std::cout << "using fmt::format\n";
#endif

    test("abc", "test");
    test("abc", "test");
}

使用VS2022 17.3.5构建,输出结果为:
C:\Temp\VS2022_17.4\x64>C:\Temp\VS2022_17.4\x64\Debug\VS2022_17.4.exe
_MSC_FULL_VER: 193331630
using fmt::format

C:\Temp\VS2022_17.4\x64>C:\Temp\VS2022_17.4\x64\Release\VS2022_17.4.exe
_MSC_FULL_VER: 193331630
using fmt::format

C:\Temp\VS2022_17.4\x64>C:\Temp\VS2022_17.4\x64\Release_StdFormat\VS2022_17.4.exe
_MSC_FULL_VER: 193331630
using std::format

使用VS2022 17.4.0构建,输出结果为
C:\temp\VS2022_17.4\x64>C:\temp\VS2022_17.4\x64\Debug\VS2022_17.4.exe
_MSC_FULL_VER: 193431933
using fmt::format

C:\temp\VS2022_17.4\x64>C:\temp\VS2022_17.4\x64\Release\VS2022_17.4.exe
_MSC_FULL_VER: 193431933
using fmt::format
FAILED II: 'abc.testð_àô ' != 'abc.test'
FAILED II: 'abc.testð_àô ' != 'abc.test'

C:\temp\VS2022_17.4\x64>C:\temp\VS2022_17.4\x64\Release_StdFormat\VS2022_17.4.exe
_MSC_FULL_VER: 193431933
using std::format

因为它在Visual Studio的两个版本中都与std::format配合使用,并且在VS2022 17.3.6版本中也与两个格式版本一起工作,所以我不知道这是Visual Studio、fmtlib还是我的代码的问题。

关于您的包含文件,有一个预处理器测试#if __has_include(include-file)。 - engf-010
2
如果你认为这是编译器的bug(在我看来是这样的),在Visual Studio中有一个反馈按钮,在右上角(关闭按钮下方),它会自动重定向你到网页,你可以在那里报告问题(可能已经被报告过了)。 - engf-010
2
17.4.0中最有用的新功能是他们使回滚更新变得容易了。看起来你会需要它,解决谁负责敏捷地修复这个问题往往需要一段时间。https://www.neowin.net/news/visual-studio-2022-174-lets-you-roll-back-to-a-previous-version/ - Hans Passant
2
我向微软报告了这个问题:https://developercommunity.visualstudio.com/t/Code-using-fmtlib-format-breaks-after-Vi/10198870 - Stefan F.
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接