将 operator<< 覆盖与位于命名空间下的操作数一起使用时,我遇到了错误。我什至不确定是什么导致了这个问题,请查看源代码:
#ifndef ENUM_UTILS_H
#define ENUM_UTILS_H
#include <sstream>
#include <unordered_map>
#include <type_traits>
#include <stdexcept>
#include <algorithm>
namespace sj
{
template <typename T>
struct enum_hasher
{
size_t operator()(T e) const
{
return static_cast<typename std::underlying_type<T>::type>(e);
}
};
template <typename T>
struct enum_strings
{
typedef const std::unordered_map<T, std::string,
enum_hasher<T>> maptype;
static maptype data;
};
template <typename T>
typename std::enable_if<std::is_enum<T>::value, std::string>::type
enum_to_string(T e)
{
return enum_strings<T>::data.at(e);
}
template <typename T>
typename std::enable_if<std::is_enum<T>::value, T>::type
string_to_enum(const std::string& str)
{
auto& map = enum_strings<T>::data;
auto got = std::find_if(map.begin(), map.end(),
[&](const std::pair<T, std::string>& x) { return x.second == str; });
if (got != map.end())
{
return got->first;
}
std::stringstream ss;
ss << "conversion from " << str << " to enum failed";
throw std::invalid_argument(ss.str());
}
} // namespace sj
/******* OPERATORS ON GLOBAL NS ********/
template <typename T>
typename std::enable_if<std::is_enum<T>::value, std::ostream&>::type
operator<<(std::ostream& os, T e)
{
return os << sj::enum_strings<T>::data.at(e);
}
#endif // ENUM_UTILS_H
// logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include <boost/log/common.hpp>
enum severity_level
{
sev_debug,
sev_info,
sev_warning,
sev_error,
sev_fatal
};
// this macro returns the logger stream
// example: LOG(debug) << "stuff to log";
#define LOG(lvl) BOOST_LOG_SEV(Logger::getLogger(), lvl)
class Logger
{
public:
typedef boost::log::sources::severity_logger<severity_level> MyLogger;
static void initialize();
static MyLogger& getLogger() { return _logger; }
private:
static bool _initialized;
static MyLogger _logger;
};
#endif // LOGGER_H
//// logger.cpp
#include <iostream>
#include "boost/log/common.hpp"
#include "boost/log/expressions.hpp"
#include "boost/log/utility/setup/file.hpp"
#include "boost/log/utility/setup/console.hpp"
#include "boost/log/utility/setup/common_attributes.hpp"
#include "boost/log/attributes/timer.hpp"
#include "boost/log/attributes/named_scope.hpp"
#include "boost/log/sources/logger.hpp"
#include "boost/log/support/date_time.hpp"
bool Logger::_initialized = false;
Logger::MyLogger Logger::_logger = Logger::MyLogger();
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
static const char* const str[] =
{
"debug",
"info",
"warning",
"error",
"fatal"
};
if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast< int >(lvl);
return strm;
}
#include <string>
enum class TestEnum
{
ValA,
ValB
};
template<> sj::enum_strings<TestEnum>::maptype
sj::enum_strings<TestEnum>::data =
{
{TestEnum::ValA, "ValA"},
{TestEnum::ValB, "ValB"}
};
namespace myns
{
enum class TestEnumNS
{
ValC,
ValD
};
}
template<> sj::enum_strings<myns::TestEnumNS>::maptype
sj::enum_strings<myns::TestEnumNS>::data =
{
{myns::TestEnumNS::ValC, "ValC"},
{myns::TestEnumNS::ValD, "ValD"}
};
int main()
{
LOG(sev_debug) << TestEnum::ValB;
std::cout << TestEnum::ValB << std::endl;
// this line doesn't compile for some reason!
LOG(sev_debug) << myns::TestEnumNS::ValC;
// std::cout works without problems
std::cout << myns::TestEnumNS::ValC << std::endl;
return 0;
}
使用 MSVC2013 编译时的完整编译日志(根据要求):
C:\build-env\boost\master\include\boost/log/utility/formatting_ostream.hpp(710) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const myns::TestEnumNS' (or there is no acceptable conversion)
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(498): could be 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_streambuf<char,std::char_traits<char>> *)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(478): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(const void *)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(458): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long double)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(438): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(418): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(float)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(397): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned __int64)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(377): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(__int64)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(356): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned long)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(336): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(long)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(316): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned int)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(291): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(int)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(271): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(unsigned short)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(237): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(short)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(217): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::_Bool)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(210): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(203): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(197): or 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_ostream<char,std::char_traits<char>> &(__cdecl *)(std::basic_ostream<char,std::char_traits<char>> &))'
C:\build-env\boost\master\include\boost/log/attributes/attribute_name.hpp(175): or 'std::basic_ostream<char,std::char_traits<char>> &boost::log::v2s_mt_nt5::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const boost::log::v2s_mt_nt5::attribute_name &)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(699): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(746): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(784): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(831): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(957): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const signed char *)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(964): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,signed char)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(971): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const unsigned char *)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(978): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,unsigned char)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(988): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>,myns::TestEnumNS>(std::basic_ostream<char,std::char_traits<char>> &&,const _Ty &)'
with
[
_Ty=myns::TestEnumNS
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\ostream(1026): or 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const std::error_code &)'
while trying to match the argument list '(std::basic_ostream<char,std::char_traits<char>>, const myns::TestEnumNS)'
..\BoostLogTest\boostlogtest.cpp(190) : see reference to function template instantiation 'boost::log::v2s_mt_nt5::basic_formatting_ostream<char,std::char_traits<char>,std::allocator<char>> &boost::log::v2s_mt_nt5::operator <<<char,std::char_traits<char>,std::allocator<char>,myns::TestEnumNS>(boost::log::v2s_mt_nt5::basic_formatting_ostream<char,std::char_traits<char>,std::allocator<char>> &,const T &)' being compiled
with
[
T=myns::TestEnumNS
]
Boost.Log 中有错误还是我做错了什么?
升压版本是 1.55。