专门针对 boost::property_maps 中的枚举 boost::lexical_cast
specializing boost::lexical_cast for enums in boost::property_maps
我正在尝试使用在其捆绑顶点 属性 内有一个 enum
的 boost::graph
。当我尝试将捆绑的 属性 用于 boost::dynamic_property 时,问题就开始了。看起来我无法获得 boost::lexical_cast 的正确模板特化来识别类型。
#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
enum MyEnum { A, B, C };
namespace boost{
template<>
std::string lexical_cast(const MyEnum & m)
{
return std::to_string(int(m));
}
} /* boost */
struct EnumWrapper
{
MyEnum e;
EnumWrapper() = default;
EnumWrapper(MyEnum e) : e{e}
{}
};
int main()
{
using namespace boost;
// This works
MyEnum e{MyEnum::B};
std::cout << lexical_cast<std::string>(e) << std::endl;
// This doesn't compile
adjacency_list<vecS, vecS, directedS, EnumWrapper> graph;
dynamic_properties dp;
dp.property("test", get(&EnumWrapper::e, graph));
return 0;
}
如果我尝试直接写 enum
,一切正常。当尝试将其放入动态属性时,出现以下错误:
In file included from /usr/include/boost/iterator/iterator_categories.hpp:22:0,
from /usr/include/boost/iterator/iterator_facade.hpp:14,
from /usr/include/boost/range/iterator_range_core.hpp:27,
from /usr/include/boost/lexical_cast.hpp:30,
from main.cpp:3:
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp: In instantiation of ‘struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<MyEnum> >’:
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:270:89: required from ‘struct boost::detail::deduce_target_char<MyEnum>’
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:407:92: required from ‘struct boost::detail::lexical_cast_stream_traits<std::__cxx11::basic_string<char>, MyEnum>’
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:468:15: required from ‘struct boost::detail::lexical_converter_impl<MyEnum, std::__cxx11::basic_string<char> >’
/usr/include/boost/lexical_cast/try_lexical_convert.hpp:181:44: required from ‘bool boost::conversion::detail::try_lexical_convert(const Source&, Target&) [with Target = MyEnum; Source = std::__cxx11::basic_string<char>]’
/usr/include/boost/lexical_cast.hpp:41:60: required from ‘Target boost::lexical_cast(const Source&) [with Target = MyEnum; Source = std::__cxx11::basic_string<char>]’
/usr/include/boost/property_map/dynamic_property_map.hpp:44:38: required from ‘Value boost::detail::read_value(const string&) [with Value = MyEnum; std::__cxx11::string = std::__cxx11::basic_string<char>]’
/usr/include/boost/property_map/dynamic_property_map.hpp:158:64: required from ‘void boost::detail::dynamic_property_map_adaptor<PropertyMap>::do_put(const boost::any&, const boost::any&, mpl_::bool_<true>) [with PropertyMap = boost::vec_adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>, boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>*, MyEnum, MyEnum&, MyEnum EnumWrapper::*>]’
/usr/include/boost/property_map/dynamic_property_map.hpp:186:11: required from ‘void boost::detail::dynamic_property_map_adaptor<PropertyMap>::put(const boost::any&, const boost::any&) [with PropertyMap = boost::vec_adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>, boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>*, MyEnum, MyEnum&, MyEnum EnumWrapper::*>]’
main.cpp:40:1: required from here
/usr/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),
Target type is neither std::istreamable nor std::wistreamable
行向我暗示,lexical_cast 没有正确专门化。然而,直接使用词法转换似乎工作得很好。
Target = MyEnum; Source = std::__cxx11::basic_string<char>
。 Boost 正在尝试将 从 string
转换为 MyEnum
;你只提供了相反的方向。
您需要提供一个转换 从 string
到 MyEnum
:
namespace boost{
template<>
MyEnum lexical_cast(const std::string & s) {
return MyEnum::A; // for example
}
} /* boost */
我正在尝试使用在其捆绑顶点 属性 内有一个 enum
的 boost::graph
。当我尝试将捆绑的 属性 用于 boost::dynamic_property 时,问题就开始了。看起来我无法获得 boost::lexical_cast 的正确模板特化来识别类型。
#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
enum MyEnum { A, B, C };
namespace boost{
template<>
std::string lexical_cast(const MyEnum & m)
{
return std::to_string(int(m));
}
} /* boost */
struct EnumWrapper
{
MyEnum e;
EnumWrapper() = default;
EnumWrapper(MyEnum e) : e{e}
{}
};
int main()
{
using namespace boost;
// This works
MyEnum e{MyEnum::B};
std::cout << lexical_cast<std::string>(e) << std::endl;
// This doesn't compile
adjacency_list<vecS, vecS, directedS, EnumWrapper> graph;
dynamic_properties dp;
dp.property("test", get(&EnumWrapper::e, graph));
return 0;
}
如果我尝试直接写 enum
,一切正常。当尝试将其放入动态属性时,出现以下错误:
In file included from /usr/include/boost/iterator/iterator_categories.hpp:22:0,
from /usr/include/boost/iterator/iterator_facade.hpp:14,
from /usr/include/boost/range/iterator_range_core.hpp:27,
from /usr/include/boost/lexical_cast.hpp:30,
from main.cpp:3:
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp: In instantiation of ‘struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<MyEnum> >’:
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:270:89: required from ‘struct boost::detail::deduce_target_char<MyEnum>’
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:407:92: required from ‘struct boost::detail::lexical_cast_stream_traits<std::__cxx11::basic_string<char>, MyEnum>’
/usr/include/boost/lexical_cast/detail/converter_lexical.hpp:468:15: required from ‘struct boost::detail::lexical_converter_impl<MyEnum, std::__cxx11::basic_string<char> >’
/usr/include/boost/lexical_cast/try_lexical_convert.hpp:181:44: required from ‘bool boost::conversion::detail::try_lexical_convert(const Source&, Target&) [with Target = MyEnum; Source = std::__cxx11::basic_string<char>]’
/usr/include/boost/lexical_cast.hpp:41:60: required from ‘Target boost::lexical_cast(const Source&) [with Target = MyEnum; Source = std::__cxx11::basic_string<char>]’
/usr/include/boost/property_map/dynamic_property_map.hpp:44:38: required from ‘Value boost::detail::read_value(const string&) [with Value = MyEnum; std::__cxx11::string = std::__cxx11::basic_string<char>]’
/usr/include/boost/property_map/dynamic_property_map.hpp:158:64: required from ‘void boost::detail::dynamic_property_map_adaptor<PropertyMap>::do_put(const boost::any&, const boost::any&, mpl_::bool_<true>) [with PropertyMap = boost::vec_adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>, boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>*, MyEnum, MyEnum&, MyEnum EnumWrapper::*>]’
/usr/include/boost/property_map/dynamic_property_map.hpp:186:11: required from ‘void boost::detail::dynamic_property_map_adaptor<PropertyMap>::put(const boost::any&, const boost::any&) [with PropertyMap = boost::vec_adj_list_vertex_property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>, boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, EnumWrapper>*, MyEnum, MyEnum&, MyEnum EnumWrapper::*>]’
main.cpp:40:1: required from here
/usr/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),
Target type is neither std::istreamable nor std::wistreamable
行向我暗示,lexical_cast 没有正确专门化。然而,直接使用词法转换似乎工作得很好。
Target = MyEnum; Source = std::__cxx11::basic_string<char>
。 Boost 正在尝试将 从 string
转换为 MyEnum
;你只提供了相反的方向。
您需要提供一个转换 从 string
到 MyEnum
:
namespace boost{
template<>
MyEnum lexical_cast(const std::string & s) {
return MyEnum::A; // for example
}
} /* boost */