我听说临时对象只能分配给常量引用。
但是这段代码给出了错误
#include <iostream.h>
template<class t>
t const& check(){
return t(); //return a temporary object
}
int main(int argc, char** argv){
const int &resCheck = check<int>(); /* fine */
typedef int& ref;
const ref error = check<int>(); / *error */
return 0;
}
得到的错误是invalid initialization of reference of type 'int&' from expression of type 'const int'