2

看起来这应该是合法的:

decltype(declval<istream>().operator>>(declval<istream>(), declval<int>())) test;

但是当我尝试编译时,我得到:

错误 C2661: std::basic_istream<char,std::char_traits<char>>::operator >>: 没有重载函数需要 2 个参数

难道我做错了什么?为什么这不评估为istream

编辑:

已经指出,因为istream& istream::operator>>(int&)是一个方法,所以第一个值是自动传递的。

但是:decltype(declval<istream>().operator>>(declval<int>())) test;错误:

错误 C2664: std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_streambuf<char,std::char_traits<char>> *): 无法将参数 1 从转换std::ios_base::iostatestd::basic_istream<char,std::char_traits<char>> &(__cdecl *)(std::basic_istream<char,std::char_traits<char>> &)

decltype(istream::operator >> (declval<istream>(), declval<int>())) test;错误:

错误 C2661: std::basic_istream<char,std::char_traits<char>>::operator >>: 没有重载函数需要 2 个参数

4

1 回答 1

7

operator>>接受 an 的是int一个成员函数(您当前正在使用成员函数非成员函数的语法)并且它通过引用获取其参数(以便它可以填充它 -declval<int>()给你一个int&&,你需要declval<int&>()得到一个int&):

using T = decltype(declval<istream>().operator>>(declval<int&>()));

更好的是不直接调用运算符,因此您不必担心哪个operator<<是成员,哪个不是:

using T = decltype(declval<istream&>() >> declval<int&>());
于 2017-01-17T12:56:33.380 回答