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开发者_StackOverflow社区 read<int>. Is there any way to get it to deduce the type from the argument?
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
.
精彩评论