0

所以,我有这个模板类,我正在尝试为其编写一个通用转换运算符。我想出的是这个(不起作用:“错误 - '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);
}

我做错了什么还是不可能?

4

1 回答 1

1

设法找到我自己的解决方案:

    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);
    }
于 2012-09-07T12:37:57.470 回答