Is "reference" a (different?) type?

N

Neelesh Bodas

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'?

-Neelesh
 
T

Thorns

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'?

-Neelesh

I believe it is 'reference to int', just like with int *p, where p is
a 'pointer to int'.
And it is a data type, because you couldn't use a int as a function's
argument, if in fact it requires, by declaration, a reference to
<data_type> or a pointer to <data_type>.
 
N

Neelesh Bodas

I believe it is 'reference to int', just like with int *p, where p is
a 'pointer to int'.
And it is a data type, because you couldn't use a int as a function's
argument, if in fact it requires, by declaration, a reference to
<data_type> or a pointer to <data_type>.

That is not correct.

void f(int&); // expects a "reference to int"
void g(const int&) // expects a "reference to const int"

int main()
{
int s = 10;
f(s); // works.
g(10); // works
g(s); // works
f(10); // doesnot work for altogether different reason.
}


-N
 
K

Kai-Uwe Bux

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
 
R

Roland Pibinger

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.

Interesting. After initialization a reference is merely an 'alias',
i.e. an 'alternate name', for the referenced object
(http://www.parashift.com/c++-faq-lite/references.html). The type of
the reference becomes indistinguishable from the type of the
referenced object after initialization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top