相关疑难解决方法(0)

在c ++中使用typedef和templates进行常量引用

我听说临时对象只能分配给常量引用.

但是这段代码给出了错误

#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;
}
Run Code Online (Sandbox Code Playgroud)

得到的错误是 invalid initialization of reference of type 'int&' from expression of type 'const int'

c++ templates const reference temporary

15
推荐指数
3
解决办法
4434
查看次数

标签 统计

c++ ×1

const ×1

reference ×1

templates ×1

temporary ×1