constant references with typedef and templates in c++

Viewed 5262

I heard the temporary objects can only be assigned to constant references.

But this code gives error

#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;
}

The error that is get is invalid initialization of reference of type 'int&' from expression of type 'const int'

5 Answers
Related