Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the type be deduced in this template function?

template<typename T>
std::istream & read(std::istream & istr, typename std::enable_if<std::is_pod<T>::value, T>::type & value)
{
    return istr.read( reinterpret_cast<char*>(&value), sizeof(T));
}

int main() 
{
    int x;
    read(cin, x); // error here
}


error C2783: 'std::istream &read(std::istream &,std::enable_if<std::tr1::is_pod<_Ty>::value,T>::type &)' : could not deduce template argument for 'T'

It works if I specify read<int>. Is there any way to get it to deduce the type from the argument?

like image 325
Benjamin Lindley Avatar asked Dec 08 '22 01:12

Benjamin Lindley


1 Answers

template<typename T>
std::istream & read(std::istream & istr, T value, 
                    typename std::enable_if<std::is_pod<T>::value>::type* = 0)
{
    return istr.read( reinterpret_cast<char*>(&value), sizeof(T));
}

Or

template<typename T>
typename std::enable_if<std::is_pod<T>::value, std::istream>::type &
read(std::istream & istr, T value)
{
    return istr.read( reinterpret_cast<char*>(&value), sizeof(T));
}

The reason yours does not work is because it is not sufficient for determining T if you know the type of the argument. What if enable_if would be a template like the following?

template<int N, typename T> struct A { typedef int type; };

Any T in <std::is_pod<T>::value, T> would do it. In general, a function parameter type formed by ...T...::type is called a non-deduced context and can't be used to deduce T.

like image 181
Johannes Schaub - litb Avatar answered Dec 31 '22 09:12

Johannes Schaub - litb