gpt4 book ai didi

c++ - 命名空间的 Boost.Log 错误

转载 作者:行者123 更新时间:2023-11-28 00:39:13 25 4
gpt4 key购买 nike

我在对位于命名空间下的操作数使用 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 中是否存在错误或我做错了什么?

Boost 版本为 1.55。

最佳答案

查找失败的运算符不直接是这里的运算符:

LOG(sev_debug) << myns::TestEnumNS::ValC;

但是,正如 clang++ 所说:

/usr/local/include/boost/log/utility/formatting_ostream.hpp:664:19: error: invalid operands to binary expression ('ostream_type' (aka 'basic_ostream') and 'myns::TestEnumNS')

strm.stream() << value;
~~~~~~~~~~~~~ ^ ~~~~~

这个表达式可能找不到operator<<全局命名空间中的函数,如果围绕此表达式的命名空间中的任何其他重载停止不合格查找。

仔细查看文件 formatting_ostream.hpp , 这个表达式出现在 boost::log 里面命名空间。任何名称为 operator<< 的函数在boost::logboost命名空间将停止不合格查找并防止不合格查找找到您的 operator<<在全局命名空间中声明。

您应该以可以通过 ADL 找到的方式提供您的运营商,例如

namespace myns
{

enum class TestEnumNS
{
ValC,
ValD
};

using ::operator<<;

}

注意我目前不完全确定这是否足够,或者您是否必须在 myns 中编写适当的函数。 clang++ 使用此添加编译代码,但会发出大量链接器错误。

关于c++ - 命名空间的 Boost.Log 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19699542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com