Is "reference" a (different?) type?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neelesh Bodas

    Is "reference" a (different?) type?

    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

  • Thorns

    #2
    Re: Is "reference " a (different?) type?

    On Jul 14, 9:57 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
    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_typeor a pointer to <data_type>.

    Comment

    • Neelesh Bodas

      #3
      Re: Is &quot;reference &quot; a (different?) type?

      On Jul 15, 12:11 am, Thorns <brokenth...@gm ail.comwrote:
      >
      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_typeor 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

      Comment

      • Kai-Uwe Bux

        #4
        Re: Is &quot;reference &quot; a (different?) type?

        Neelesh Bodas wrote:
        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>::v alue() );
        }

        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

        Comment

        • Roland Pibinger

          #5
          Re: Is &quot;reference &quot; a (different?) type?

          On Sun, 15 Jul 2007 06:08:27 +0200, Kai-Uwe Bux wrote:
          >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 indistinguishab le from the type of the
          referenced object after initialization.


          --
          Roland Pibinger
          "The best software is simple, elegant, and full of drama" - Grady Booch

          Comment

          Working...