c++ - C++11: Implementing a Selector between std::array and std::unordered_map according to item count -
i'm trying create mapping class automatically select proper implementation according possible value range. if value range 256, use std::array, otherwise, use std::unordered_map.
template<typename keytype, typename valtype> class mapimpl : public std::unordered_map<keytype,valtype> { void foo() { std::cout << "map foo" << std::endl; } }; template<typename valtype, std::size_t size> class arrimpl : public std::array<valtype,size> { public: void foo() { std::cout << "arr foo" << std::endl; } }; template<typename keytype, typename valtype, keytype min_value = boost::integer_traits<keytype>::const_min, keytype max_value = boost::integer_traits<keytype>::const_max> class mappingselector { public: void bar() { map.foo(); } private: typedef std::conditional<(max_value-min_value < 256), arrimpl<valtype,max_value-min_value+1>, mapimpl<keytype,valtype> > mappingtype; mappingtype map; }; so far good(?) instantiate with:
mappingselector<unsigned char, double> ms; ms.bar(); however, compiler doesn't seem , tells me mappingtype has no member named foo.
std::conditional doesn't define directly resulting type, defines struct has typedef type corresponding type.
this means must qualify specific type, typedef should
typedef std::conditional<(max_value-min_value < 256), arrimpl<valtype,max_value-min_value+1>, mapimpl<keytype,valtype> > mappingtype; typename mappingtype::type map; ^ requires typename unambiguation ^ ::type real typedef or can typedef mappingtype directly qualifying ::type, same result. actually, since working c++11 can use using, eg:
using mappingtype = typename std::conditional<(max_value-min_value < 256), arrimpl<valtype,max_value-min_value+1>, mapimpl<keytype,valtype> >::type; mappingtype map;
Comments
Post a Comment