Question

template< typename charT >
struct serializer
{
    /* ... */
private:
    std::basic_string< charT > base64_encode( unsigned charT * bytes, unsigned length );
};

I would like the private member function to take an unsigned whatever-char-type. If charT is char16_t, char32_t, wchar_t, I want the signature to be unsigned that. How can I do that?

Was it helpful?

Solution

The easiest way is by usage of std::make_unsigned, which works for any integral type:

template<typename T>
typename std::make_unsigned<T>::type convert(T const& input)
{
    return static_cast<typename std::make_unsigned<T>::type>(input);
}

Using this in your class becomes fairly ugly when used directly, so we will add a type alias:

template< typename charT >
struct serializer
{
    typedef typename ::std::make_unsigned<charT>::type ucharT;
    /* ... */
private:
    std::basic_string< ucharT > base64_encode( ucharT * bytes, unsigned length );
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top