【发布时间】:2016-08-15 14:55:14
【问题描述】:
这是一个测试用例:
#include <istream>
#include <boost/lexical_cast.hpp>
namespace N {
enum class alarm_code_t {
BLAH
};
}
std::istream& operator>>(std::istream& is, N::alarm_code_t& code)
{
std::string tmp;
is >> tmp;
if (tmp == "BLAH")
code = N::alarm_code_t::BLAH;
else
is.setstate(std::ios::failbit);
return is;
}
int main()
{
auto code = boost::lexical_cast<N::alarm_code_t>("BLAH");
}
Boost 拒绝转换,声称没有匹配的operator>>:
In file included from /usr/local/include/boost/iterator/iterator_categories.hpp:22:0,
from /usr/local/include/boost/iterator/iterator_facade.hpp:14,
from /usr/local/include/boost/range/iterator_range_core.hpp:27,
from /usr/local/include/boost/lexical_cast.hpp:30,
from main.cpp:2:
/usr/local/include/boost/lexical_cast/detail/converter_lexical.hpp: In instantiation of 'struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<N::alarm_code_t> >':
/usr/local/include/boost/lexical_cast/detail/converter_lexical.hpp:270:89: required from 'struct boost::detail::deduce_target_char<N::alarm_code_t>'
/usr/local/include/boost/lexical_cast/detail/converter_lexical.hpp:404:92: required from 'struct boost::detail::lexical_cast_stream_traits<const char*, N::alarm_code_t>'
/usr/local/include/boost/lexical_cast/detail/converter_lexical.hpp:465:15: required from 'struct boost::detail::lexical_converter_impl<N::alarm_code_t, const char*>'
/usr/local/include/boost/lexical_cast/try_lexical_convert.hpp:174:44: required from 'bool boost::conversion::detail::try_lexical_convert(const Source&, Target&) [with Target = N::alarm_code_t; Source = char [5]]'
/usr/local/include/boost/lexical_cast.hpp:42:60: required from 'Target boost::lexical_cast(const Source&) [with Target = N::alarm_code_t; Source = char [5]]'
main.cpp:25:60: required from here
/usr/local/include/boost/lexical_cast/detail/converter_lexical.hpp:243:13: error: static assertion failed: Target type is neither std::istream`able nor std::wistream`able
BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, T >::value),
(demo)
但是,当我在命名空间 N 内声明/定义 operator>> 时,代码的工作方式与宣传的一样。
这是为什么呢?为什么查找会失败?
【问题讨论】:
-
沼泽标准 ADL 问题?
-
@T.C.: 嗯...在命名空间
N中找到其他operator>>,因此未搜索全局命名空间并且未找到此特定operator>>?但我在N中没有另一个operator>>。或者任何,事实上。我无法理解 ADL 的来源。 -
ADL 进来是因为你的
operator>>的第二个参数类型是N::alarm_code_t,所以N是一个关联的命名空间,会搜索运算符定义。 -
@Praetorian
>>调用发生在namespace boost。我认为 LRiO 的问题是“为什么在namespace boost中调用>>时不考虑namespace ::中的operator>>也?”说“ADL 没有找到它”只回答了一半的问题:ADL 不是唯一的查找。
标签: c++ c++11 boost lexical-cast