为什么fmt::format不接受字符串作为参数



我以前已经成功地使用了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 FilesCMakebincmake.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]buildvcpkg_installedx64-windowsincludefmtcore.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]builddrakengdrakeng.vcxproj]
[build] [WORKSPACE]buildvcpkg_installedx64-windowsincludefmtcore.h(1070): message : while compiling class template member function 'int fmt::v7::detail::arg_mapper<Context>::map(...)' [[WORKSPACE]builddrakengdrakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]buildvcpkg_installedx64-windowsincludefmtcore.h(1259): message : see reference to function template instantiation 'int fmt::v7::detail::arg_mapper<Context>::map(...)' being compiled [[WORKSPACE]builddrakengdrakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]buildvcpkg_installedx64-windowsincludefmtcore.h(1084): message : see reference to class template instantiation 'fmt::v7::detail::arg_mapper<Context>' being compiled [[WORKSPACE]builddrakengdrakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]buildvcpkg_installedx64-windowsincludefmtcore.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]builddrakengdrakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]buildvcpkg_installedx64-windowsincludefmtcore.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]builddrakengdrakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]buildvcpkg_installedx64-windowsincludefmtcore.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]builddrakengdrakeng.vcxproj]
[build] [WORKSPACE]drakenglogging.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]builddrakengdrakeng.vcxproj]
[build]           with
[build]           [
[build]               S=std::string
[build]           ]
[build] Build finished with exit code 1

这些错误听起来像是fmt没有fmt::formatter<std::string>专业化,这对我来说不太合适,所以我要么配置错误,要么没有包含标头之类的东西。坦率地说,我很困惑。一旦我有了答案,知道我的运气可能会很傻。无论如何,提前感谢您提供的任何帮助。

好吧,很明显,这个错误并不是由我认为的原因引起的(谢谢,Visual C++编译器错误消息,非常有用!/s(。因此,为了解释实际发生了什么:

  • Logger::Info调用一个更通用的函数Logger::Log(LoggingLevel level, const std::string & message)
  • Logger::Log还调用其内部的fmt::format来格式化一些附加信息以保存到日志中
  • fmt::format调用的自变量之一是level,然后需要将其转换为字符串(我选择了生成ToString函数,而不是为其生成fmt::formatter(
  • 问题是CCD_ 12函数将CCD_;干净";处理无效值。很明显,我忘了对返回值调用.value_or,而TH是导致错误的原因

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

最新更新