user1233963
user1233963

Reputation: 1480

Templated explicit conversion operator

So, I have this template class for which I'm trying to write a generic conversion operator. What I've come up with is this ( doesn't work: "Error - expected a qualified name after 'typename'" ):

template <typename T>
class object{
...
T internal;
...
template <typename U>
explicit operator typename decltype(
std::conditional< 
     std::is_convertible<T, U>::type , U, T>::type)()
{
return static_cast<std::conditional<std::is_convertible<T, U>::type ,U, T>::type>(internal);
}

Am I doing something wrong or is it just not possible?

Upvotes: 0

Views: 257

Answers (1)

user1233963
user1233963

Reputation: 1480

Managed to find a solution of my own:

    template <typename U>
    explicit operator typename decltype(std::conditional< 
                                        std::is_convertible<T, U>::type , 
                                        U, 
                                        T>::type)::value_type ()

    {
        return static_cast<typename decltype(std::conditional< 
                        std::is_convertible<T, U>::type , 
                        U, 
                        T>::type)::value_type>(internal);
    }

Upvotes: 1

Related Questions