c++ - how can I use std::enable_if in a conversion operator? -
basically want range type implicitly convertible range<const char>
range<const unsigned char>
. std::enable_if seems impossible because function takes no arguments , has no return. whats work around?
here tried:
template<typename t> class range{ t* begin_; t* end_; public: range(t* begin,t* end):begin_{begin},end_{end}{} template<int n> range(t (&a)[n]):begin_{static_cast<t*>(&a[0])},end_{static_cast<t*>(&a[n-1])}{} t* begin(){return begin_;} t* end(){return end_;} operator typename std::enable_if<std::is_same<t,const char>::value,range<const unsigned char>&>::type (){ return *reinterpret_cast<range<const unsigned char>*>(this); } };
make template dummy parameter defaults t - postpone type deduction point function gets instantiated, otherwise sfinae doesn't work. check want in default value of parameter.
template< typename u = t, typename = typename std::enable_if< std::is_same<u,const char>::value >::type > operator range<const unsigned char>() { return *reinterpret_cast<range<const unsigned char>*>(this); }
Comments
Post a Comment