如何为提升日志和自定义类型定义输出流运算符
How to define output stream operator for boost log and a custom type
我能够为一个简单的结构定义输出流运算符,但是,不能为 std::array。以下代码编译失败。出了什么问题,我该如何解决?
#include <array>
#include <iostream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
using hash_t = std::array< unsigned char, 32 >;
std::ostream& operator<< ( std::ostream& os, hash_t const& arr )
{
os << "ole!";
return os;
}
int main(int, char*[])
{
hash_t arr;
std::cerr << arr << std::endl; // complies cleanly
BOOST_LOG_TRIVIAL(debug) << arr; // Error
return 0;
}
错误来了。
GCC(提升 1.55,gcc-4.9.2(:
In file included from /usr/include/boost/log/sources/record_ostream.hpp:31:0,
from /usr/include/boost/log/trivial.hpp:23,
from trival.cpp:4:
/usr/include/boost/log/utility/formatting_ostream.hpp: In instantiation of ‘boost::log::v2s_mt_posix::basic_formatting_ostream<CharT, TraitsT, AllocatorT>& boost::log::v2s_mt_posix::operator<<(boost::log::v2s_mt_posix::basic_formatting_ostream<CharT, TraitsT, AllocatorT>&, const T&) [with CharT = char; TraitsT = std::char_traits<char>; AllocatorT = std::allocator<char>; T = std::array<unsigned char, 32ul>]’:
trival.cpp:18:30: required from here
/usr/include/boost/log/utility/formatting_ostream.hpp:710:19: error: cannot bind ‘boost::log::v2s_mt_posix::basic_formatting_ostream<char>::ostream_type {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
strm.stream() << value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from trival.cpp:2:
/usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::array<unsigned char, 32ul>]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
叮当错误(升压 1.64,叮当-800.0.42.1(:
In file included from /usr/local/include/boost/log/trivial.hpp:23:
In file included from /usr/local/include/boost/log/sources/record_ostream.hpp:36:
/usr/local/include/boost/log/utility/formatting_ostream.hpp:878:19: error: invalid operands to binary expression ('ostream_type' (aka
'basic_ostream<char, std::__1::char_traits<char> >') and 'std::__1::array<unsigned char, 32>')
strm.stream() << value;
~~~~~~~~~~~~~ ^ ~~~~~
/usr/local/include/boost/log/sources/record_ostream.hpp:390:51: note: in instantiation of function template specialization
'boost::log::v2_mt_posix::operator<<<boost::log::v2_mt_posix::basic_formatting_ostream<char, std::__1::char_traits<char>, std::__1::allocator<char> >,
std::__1::array<unsigned char, 32> >' requested here
static_cast< formatting_ostream_type& >(strm) << value;
^
/Users/adam/GitPen/BoostLog/trival/trival.cpp:18:27: note: in instantiation of function template specialization
'boost::log::v2_mt_posix::operator<<<boost::log::v2_mt_posix::basic_record_ostream<char>, std::__1::array<unsigned char, 32> >' requested here
BOOST_LOG_TRIVIAL(debug) << arr;
你可能不喜欢这个答案。
要使其正常工作,您需要将operator<<
添加到namespace std
:
namespace std{
std::ostream& operator<< ( std::ostream& os, const hash_t& arr )
{/*...*/}
}
由于 ADL 的工作方式,它只会考虑operator<<
及其在namespace std
中的专业化
您不会喜欢它,因为将此功能添加到namespace std
不是合法的事情:
[命名空间.std]
如果C++程序将声明或定义添加到命名空间
std
或 命名空间 std 中的命名空间,除非另有指定。程序可以添加模板专用化 对于任何标准库模板到命名空间 std 仅当声明依赖于用户定义类型 并且专用化符合原始模板的标准库要求,并且没有明确 禁止。
也许最简单的方法是从std
类型继承(而不做其他事情(:
struct hash_t : std::array< unsigned char, 32 >{};
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 使用模板类的自定义列表类型中的内存泄漏
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 自定义引用类型
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- 标准::原子中的自定义类型
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 在Qt中注册自定义元类型的别名类型
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 如何使用自定义类型声明Arduino数组?
- 特征中的自定义标量类型
- 在点云库 (PCL) 中使用自定义点类型的问题
- 为自定义打印调试实现传递任何类型的变量
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 将成员函数的返回类型引用到C++中的自定义类