如何使用FMT库格式化指针
How to format pointers using fmt library?
我正在接受一些人的建议,并查看FMT库:http://fmtlib.net
它似乎具有我需要的功能,并声称支持%p
(指针(,但是在编译使用%P的代码时,我会收到一系列的模板错误(难以理解(。我将在此结束时发布它们。
如果我拔出%p
和相应的指针参数,则在VS2017 C 17中进行编译。
但是,我对如何解码模板错误或获得有关为什么首先不接受 %p
参数的洞察力感到茫然。
我尝试将论点施加到(void*)
-同一问题。
我尝试使用格式 {}
中的python样式语法 - 相同的问题。
我已经与其余格式分开分解了%p位 - 同一问题。
我看到对用户类型有支持 - 但是在这种情况下,我只想将其作为原始指针值输出。我可以跳过它,毕竟,指针地址真的有多有价值?但是,当然,这意味着从sprintf
转换为fmt::format
进行更多的工作,以追捕所有%p并"对它们做某事",例如Elide。
但文档似乎表明得到%p的支持-http://fmtlib.net/latest/latest/syntax.html(大约3/4 down -way down -down-搜索'pointer'或'pointer'或'p'(。<<<<<<<<<<<<<<<<(。/p>
这是调用函数:(注意:pAccels
被声明为const ACCEL *
(
m_hAccel = ::CreateAcceleratorTable(const_cast<LPACCEL>(pAccels), (int)count);
if (!m_hAccel)
{
auto error = GetLastError();
throw CWinAPIErrorException(__FUNCTION__, "CreateAcceleratorTable", fmt::format("%p,%u", pAccels, count), error);
}
这是诊断派:
1>c:usersstevesourcefmtincludefmtcore.h(1073): error C2825: 'fmt::v5::internal::get_type<Context,Arg>::value_type': must be a class or namespace when followed by '::'
1> with
1> [
1> Context=fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<char>>,char>,
1> Arg=const ACCEL *
1> ]
1>c:usersstevesourcefmtincludefmtcore.h(1081): note: see reference to class template instantiation 'fmt::v5::internal::get_type<Context,Arg>' being compiled
1> with
1> [
1> Context=fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<char>>,char>,
1> Arg=const ACCEL *
1> ]
1>c:usersstevesourcefmtincludefmtcore.h(1190): note: see reference to function template instantiation 'unsigned __int64 fmt::v5::internal::get_types<Context,const ACCEL*,size_t>(void)' being compiled
1> with
1> [
1> Context=fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<char>>,char>
1> ]
1>c:usersstevesourcefmtincludefmtcore.h(1190): note: while compiling class template member function 'unsigned __int64 fmt::v5::format_arg_store<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<Char>>,Char>,const ACCEL *,size_t>::get_types(void)'
1> with
1> [
1> Char=char
1> ]
1>c:usersstevesourcefmtincludefmtcore.h(1478): note: see reference to class template instantiation 'fmt::v5::format_arg_store<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<Char>>,Char>,const ACCEL *,size_t>' being compiled
1> with
1> [
1> Char=char
1> ]
1>c:usersstevesourcetbxwapiacceleratortable.cpp(58): note: see reference to function template instantiation 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> fmt::v5::format<char[6],const ACCEL*,size_t,0>(const S (&),const ACCEL *const &,const size_t &)' being compiled
1> with
1> [
1> S=char [6]
1> ]
1>c:usersstevesourcefmtincludefmtcore.h(1073): error C2510: 'value_type': left of '::' must be a class/struct/union
1>c:usersstevesourcefmtincludefmtcore.h(1073): error C2065: 'type_tag': undeclared identifier
1>c:usersstevesourcefmtincludefmtcore.h(1073): error C2131: expression did not evaluate to a constant
1>c:usersstevesourcefmtincludefmtcore.h(1073): note: a non-constant (sub-)expression was encountered
1>c:usersstevesourcefmtincludefmtcore.h(1197): error C2131: expression did not evaluate to a constant
1>c:usersstevesourcefmtincludefmtcore.h(1082): note: failure was caused by non-constant arguments or reference to a non-constant symbol
1>c:usersstevesourcefmtincludefmtcore.h(1082): note: see usage of 'value'
要格式化指针,您可以将其施放到void*
:
std::string s = fmt::format("{},{}", static_cast<void*>(pAccels), count);
或将其包裹在fmt::ptr
中:
std::string s = fmt::format("{},{}", fmt::ptr(pAccels), count);
在Godbolt上工作示例:https://godbolt.org/z/scnbjr
请注意,format
使用类似python的格式字符串语法,而不是 printf
's并返回 std::string
对象。
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- LLDB 格式化程序摘要提供程序和模板智能指针的语法子项
- 如何使用FMT库格式化指针
- 有没有一种C++的方法可以使用iostream格式化地址和指针
- 格式化指针的cout
- Eclipse:如何格式化指针声明