【问题标题】:Why is fmt::format not accepting a string as an argument?为什么 fmt::format 不接受字符串作为参数?
【发布时间】:2020-10-23 23:52:55
【问题描述】:

我之前已经成功使用过fmt 库,所以我不确定为什么会遇到这个问题。首先,我将向您展示产生错误的代码:

void Logger::AttachSink(const std::string & id, LogSink sink) {
    using namespace std::literals;
    auto id_found = sinks.find(id) != std::endl(sinks);
    if(!id_found) {
        sinks.insert(std::make_pair(id, sink));
        Info(
            fmt::format(
                "Sink \"{}\" Attached"s,
                id
            ) // This is the line where the error is apparently happening, and I'm assuming the `id` is the culprit for some reason.
        );
    }
}

这是构建日志(注意:我使用 VSCode 作为编辑器,使用 CMake 使用 Visual Studio 2019 的编译器构建项目):

[main] Building folder: [WORKSPACE]
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.exe" --build [WORKSPACE]/build --config Debug --target ALL_BUILD -- /maxcpucount:6
[build] Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
[build] Copyright (C) Microsoft Corporation. All rights reserved.
[build] 
[build]   logging.cpp
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1073,9): error C2338: Cannot format argument. To make type T formattable provide a formatter<T> specialization: https://fmt.dev/latest/api.html#formatting-user-defined-types [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1070): message : while compiling class template member function 'int fmt::v7::detail::arg_mapper<Context>::map(...)' [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1259): message : see reference to function template instantiation 'int fmt::v7::detail::arg_mapper<Context>::map(...)' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1084): message : see reference to class template instantiation 'fmt::v7::detail::arg_mapper<Context>' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1241): message : see reference to alias template instantiation 'fmt::v7::detail::mapped_type_constant<const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,Context>' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1400): message : see reference to function template instantiation 'unsigned __int64 fmt::v7::detail::encode_types<Context,const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,>(void)' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1834): message : see reference to class template instantiation 'fmt::v7::format_arg_store<fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>,const std::basic_string<char,std::char_traits<char>,std::allocator<char>>>' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build] [WORKSPACE]\drakeng\logging.cpp(24): message : see reference to function template instantiation 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> fmt::v7::format<std::string,const std::string&,char>(const S &,const std::string &)' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               S=std::string
[build]           ]
[build] Build finished with exit code 1

这些错误听起来像是fmt 没有fmt::formatter&lt;std::string&gt; 专业化,这对我来说听起来不正确,所以我要么配置错误,要么没有包含标题或其他东西。坦白说,我很困惑。一旦我得到答案,知道我的运气可能会很愚蠢。无论如何,提前感谢您提供的任何帮助。

【问题讨论】:

  • 是的,我意识到我可以使用串联来组合字符串,但是如果我想让格式化的字符串更复杂怎么办,当然这应该是可能的。另外,我很确定我以前用fmt 库做过这个确切的事情,所以我很困惑。

标签: c++ cmake c++17 c++20 fmt


【解决方案1】:

好的,显然错误不是由我认为的原因引起的(谢谢,Visual C++ 编译器错误消息,非常有帮助!/s)。所以解释一下实际发生的事情:

  • Logger::Info 调用更通用的函数 Logger::Log(LoggingLevel level, const std::string &amp; message)
  • Logger::Log 还在其中调用 fmt::format 以格式化一些附加信息以保存到日志中。
  • fmt::format 调用的参数之一是 level,然后需要将其转换为字符串(我选择创建 ToString 函数而不是为它创建 fmt::formatter)。
  • 问题是ToString 函数返回一个std::optional&lt;std::string&gt; 以“干净地”处理无效值。好吧,显然我忘了在返回值上调用 .value_or,而 THAT 是导致错误的原因。

不幸的是,没有任何错误消息提到与该行有关的任何内容,因此浪费了几分钟/几小时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2015-12-15
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多