【发布时间】:2011-04-17 15:35:16
【问题描述】:
听说临时对象只能分配给常量引用。
但是这段代码出错了
#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'
【问题讨论】:
标签: c++ templates reference constants temporary