2016-04-29 86 views
2

我正嘗試使用boost::graph,它的頂點屬性中包含enum。當我嘗試使用boost :: dynamic_property的捆綁屬性時,問題就開始了。看起來我無法獲得boost :: lexical_cast識別類型的正確模板專門化。專注於boost :: lexical_cast用於boost中的枚舉:: property_maps

#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的不正確的專業。但是,直接使用詞法轉換似乎很好。

+0

'Target = MyEnum; Source = std :: __ cxx11 :: basic_string '。 Boost正試圖將*從*'string' *轉換爲*'MyEnum';你只提供了相反的方向。 – ecatmur

回答

2

Target = MyEnum; Source = std::__cxx11::basic_string<char>。 Boost試圖將string轉換成MyEnum;你只提供了相反的方向。

你需要從stringMyEnum提供一個轉換

namespace boost{ 
    template<> 
    MyEnum lexical_cast(const std::string & s) { 
     return MyEnum::A; // for example 
    } 
} /* boost */ 

Example

+0

很好,對於錯誤消息中的小改動非常重要。我首先確定了另一個方向,並沒有意識到'Target'和'Source'現在已經被切換了。 – Slizzered

相關問題