Neelesh said:
Just wondering about exact terminology used by the standard to
describe a reference. More specifically, is "reference" a type?
int i = 10; // type of i is int
int &ri = i; // ri is declared as a "reference to int". But what is
type of ri? 'int' or 'reference to int'?
Tricky. Let's see how the use of references influences overload resolution
and the typeid() operator:
#include <iostream>
#include <ostream>
#include <typeinfo>
template < typename T >
struct type_name {
static
char const * value ( void ) {
return ( "unknown" );
}
};
template <>
struct type_name<int> {
static
char const * value ( void ) {
return ( "int" );
}
};
template <>
struct type_name<int&> {
static
char const * value ( void ) {
return ( "int&" );
}
};
template < typename T >
const char * typeof ( T ) {
return ( type_name<T>::value() );
}
int& int_ref ( void ) {
static int i = 5;
return ( i );
}
#define SHOW(expr) std::cout << #expr << " = " << (expr) << '\n'
int main ( void ) {
int i = 10;
int& ri = i;
SHOW( typeof(i) );
SHOW( typeof(ri) );
SHOW( ( &typeid( ri ) == &typeid(int) ) );
SHOW( typeof( int_ref() ) );
SHOW( ( &typeid( int_ref() ) == &typeid(int) ) );
}
Output:
typeof(i) = int
typeof(ri) = int
( &typeid( ri ) == &typeid(int) ) = 1
typeof( int_ref() ) = int
( &typeid( int_ref() ) == &typeid(int) ) = 1
It looks as though ri has type int for all practical purposes. Also,
whatever is returned by int_ref() masquerades really well as an int.
Best
Kai-Uwe Bux