one more template question and some typedef

Discussion in 'C++' started by Tony Johansson, Aug 15, 2005.

  1. Hello Experts!

    I have the following Array template class see below.
    I execute these three statements
    statement 1: Array<int> x(5);
    statement 2: cin >>x[1];
    statement 3: Array<int>::element_type y = x[1];
    but I can't understand the last one which is
    Array<int>::element_type y = x[1];
    We have a typedef T element_type; in the template Array class see below
    and in my example I use int as the type parameter for T so
    that would mean that it says
    Array<int>::int y = x[1];
    this is even more confusing for me.
    If the Array template class for example had a static variable with name
    number then you would
    use Array<int>::number; to access this static variable.



    template <typename T>
    class Array
    {
    public:
    Array(size_t=100); // size_t is predefined
    Array(const Array<T>&);
    Array<T>& operator=(const Array<T>&);
    virtual ~Array();
    virtual const T& operator[](int) const;
    virtual T& operator[](int);
    int size() const;

    typedef T element_type;
    protected:
    size_t size_;
    T* array;
    };

    Many thanks

    //Tony
     
    Tony Johansson, Aug 15, 2005
    #1
    1. Advertising

  2. Tony Johansson wrote:
    > I have the following Array template class see below.
    > I execute these three statements
    > statement 1: Array<int> x(5);
    > statement 2: cin >>x[1];
    > statement 3: Array<int>::element_type y = x[1];
    > but I can't understand the last one which is
    > Array<int>::element_type y = x[1];
    > We have a typedef T element_type; in the template Array class see below
    > and in my example I use int as the type parameter for T so
    > that would mean that it says
    > Array<int>::int y = x[1];
    > this is even more confusing for me.


    No, it doesn't mean it says "Array<int>::int y = x[1];".
    It means what it says,

    Array<int>::element_type y = x[1];

    or you can rewrite it to say

    int y = x[1];

    'element_type' does not mean 'int' by itself. It only means 'int' if
    used _along_ with the class in which it's defined. For example, in
    Array<string>, 'element_type' would mean 'string'. So, if you write

    Array<string>::element_type

    it would be the same as writing

    string

    > If the Array template class for example had a static variable with name
    > number then you would
    > use Array<int>::number; to access this static variable.


    Yes.

    V
     
    Victor Bazarov, Aug 15, 2005
    #2
    1. Advertising

  3. "Victor Bazarov" <> skrev i meddelandet
    news:aV4Me.30040$01.us.to.verio.net...
    > Tony Johansson wrote:
    >> I have the following Array template class see below.
    >> I execute these three statements
    >> statement 1: Array<int> x(5);
    >> statement 2: cin >>x[1];
    >> statement 3: Array<int>::element_type y = x[1];
    >> but I can't understand the last one which is
    >> Array<int>::element_type y = x[1];
    >> We have a typedef T element_type; in the template Array class see below
    >> and in my example I use int as the type parameter for T so
    >> that would mean that it says
    >> Array<int>::int y = x[1];
    >> this is even more confusing for me.

    >
    > No, it doesn't mean it says "Array<int>::int y = x[1];".
    > It means what it says,
    >
    > Array<int>::element_type y = x[1];
    >
    > or you can rewrite it to say
    >
    > int y = x[1];
    >
    > 'element_type' does not mean 'int' by itself. It only means 'int' if
    > used _along_ with the class in which it's defined. For example, in
    > Array<string>, 'element_type' would mean 'string'. So, if you write
    >
    > Array<string>::element_type
    >
    > it would be the same as writing
    >
    > string
    >
    >> If the Array template class for example had a static variable with name
    >> number then you would
    >> use Array<int>::number; to access this static variable.

    >
    > Yes.
    >
    > V


    I can't understand how Array<int>::element_type
    is the same as int in my example.
    or that Array<string>::element_type is the same as string
    Some question that might help me to understand this.
    How does the scope operation be understood here?
    What meaning have T in the typedef T element_type;?
    I want to understand the scope operatorn in such way that there is some
    variable
    in the Array template class that has name element_type.
    The fully name of the Array template class when you have int as the
    parameter is
    Array<int>
    Can you give me some more explanation about Array<int>::element_type
    that might help me to understand this.

    //Tony
     
    Tony Johansson, Aug 15, 2005
    #3
  4. Tony wrote:
    > "Victor Bazarov" <> skrev i meddelandet
    > news:aV4Me.30040$01.us.to.verio.net...
    >
    >>Tony Johansson wrote:
    >>
    >>>I have the following Array template class see below.
    >>>I execute these three statements
    >>>statement 1: Array<int> x(5);
    >>>statement 2: cin >>x[1];
    >>>statement 3: Array<int>::element_type y = x[1];
    >>>but I can't understand the last one which is
    >>>Array<int>::element_type y = x[1];
    >>>We have a typedef T element_type; in the template Array class see below
    >>>and in my example I use int as the type parameter for T so
    >>>that would mean that it says
    >>>Array<int>::int y = x[1];
    >>>this is even more confusing for me.

    >>
    >>No, it doesn't mean it says "Array<int>::int y = x[1];".
    >>It means what it says,
    >>
    >> Array<int>::element_type y = x[1];
    >>
    >>or you can rewrite it to say
    >>
    >> int y = x[1];
    >>
    >>'element_type' does not mean 'int' by itself. It only means 'int' if
    >>used _along_ with the class in which it's defined. For example, in
    >>Array<string>, 'element_type' would mean 'string'. So, if you write
    >>
    >> Array<string>::element_type
    >>
    >>it would be the same as writing
    >>
    >> string
    >>
    >>
    >>>If the Array template class for example had a static variable with name
    >>>number then you would
    >>>use Array<int>::number; to access this static variable.

    >>
    >>Yes.
    >>
    >>V

    >
    >
    > I can't understand how Array<int>::element_type
    > is the same as int in my example.


    Try a simple substitution for this struct template:

    template<class T> struct A { typedef T type; };

    If I instantiate A<int>, what happens? It's as if I declare a type

    struct A<int> /* incorrect syntax, but will do */ {
    typedef int type;
    };

    (anywhere 'T' was used inside the 'A' definition, I put 'int'). Now, what
    does 'A<int>::type' stand for? It's typedef'ed to be the same as 'int'.
    So, anywhere I want to use 'int' I _can_ use 'A<int>::type' because they
    are defined to mean the same thing.

    > or that Array<string>::element_type is the same as string
    > Some question that might help me to understand this.
    > How does the scope operation be understood here?


    It's not "the scope operation". It's what you may call a "class-wide
    member access operation". A typedef inside a class is a class-wide
    member. It has a name which you can access using the '::'. The name
    of that member in my example is 'type'. A way to access it is to supply
    the name of the _class_ with the scope resolution operator:

    A<int> :: type
    ^^^^^^ ^^ ^^^^
    Name of the class Scope resolution Member name

    > What meaning have T in the typedef T element_type;?


    Just like with any other typedef, 'element_type' is now the synonym for
    whatever 'T' is. For any particular instantiation of the template, 'T'
    means something specific. So, in any particular instantiation of your
    'Array' template, 'element_type' will mean something in particular.

    > I want to understand the scope operatorn in such way that there is some
    > variable
    > in the Array template class that has name element_type.


    I don't understand the sentence above.

    > The fully name of the Array template class when you have int as the
    > parameter is
    > Array<int>


    It's called "instantiation", not "fully name of the Array template class".

    > Can you give me some more explanation about Array<int>::element_type
    > that might help me to understand this.


    See above. And, yes, I can give you more if you ask more specific
    questions.

    V
     
    Victor Bazarov, Aug 15, 2005
    #4
  5. "Victor Bazarov" <> skrev i meddelandet
    news:bl6Me.30059$01.us.to.verio.net...
    > Tony wrote:
    >> "Victor Bazarov" <> skrev i meddelandet
    >> news:aV4Me.30040$01.us.to.verio.net...
    >>
    >>>Tony Johansson wrote:
    >>>
    >>>>I have the following Array template class see below.
    >>>>I execute these three statements
    >>>>statement 1: Array<int> x(5);
    >>>>statement 2: cin >>x[1];
    >>>>statement 3: Array<int>::element_type y = x[1];
    >>>>but I can't understand the last one which is
    >>>>Array<int>::element_type y = x[1];
    >>>>We have a typedef T element_type; in the template Array class see below
    >>>>and in my example I use int as the type parameter for T so
    >>>>that would mean that it says
    >>>>Array<int>::int y = x[1];
    >>>>this is even more confusing for me.
    >>>
    >>>No, it doesn't mean it says "Array<int>::int y = x[1];".
    >>>It means what it says,
    >>>
    >>> Array<int>::element_type y = x[1];
    >>>
    >>>or you can rewrite it to say
    >>>
    >>> int y = x[1];
    >>>
    >>>'element_type' does not mean 'int' by itself. It only means 'int' if
    >>>used _along_ with the class in which it's defined. For example, in
    >>>Array<string>, 'element_type' would mean 'string'. So, if you write
    >>>
    >>> Array<string>::element_type
    >>>
    >>>it would be the same as writing
    >>>
    >>> string
    >>>
    >>>
    >>>>If the Array template class for example had a static variable with name
    >>>>number then you would
    >>>>use Array<int>::number; to access this static variable.
    >>>
    >>>Yes.
    >>>
    >>>V

    >>
    >>
    >> I can't understand how Array<int>::element_type
    >> is the same as int in my example.

    >
    > Try a simple substitution for this struct template:
    >
    > template<class T> struct A { typedef T type; };
    >
    > If I instantiate A<int>, what happens? It's as if I declare a type
    >
    > struct A<int> /* incorrect syntax, but will do */ {
    > typedef int type;
    > };
    >
    > (anywhere 'T' was used inside the 'A' definition, I put 'int'). Now, what
    > does 'A<int>::type' stand for? It's typedef'ed to be the same as 'int'.
    > So, anywhere I want to use 'int' I _can_ use 'A<int>::type' because they
    > are defined to mean the same thing.
    >
    >> or that Array<string>::element_type is the same as string
    >> Some question that might help me to understand this.
    >> How does the scope operation be understood here?

    >
    > It's not "the scope operation". It's what you may call a "class-wide
    > member access operation". A typedef inside a class is a class-wide
    > member. It has a name which you can access using the '::'. The name
    > of that member in my example is 'type'. A way to access it is to supply
    > the name of the _class_ with the scope resolution operator:
    >
    > A<int> :: type
    > ^^^^^^ ^^ ^^^^
    > Name of the class Scope resolution Member name
    >
    >> What meaning have T in the typedef T element_type;?

    >
    > Just like with any other typedef, 'element_type' is now the synonym for
    > whatever 'T' is. For any particular instantiation of the template, 'T'
    > means something specific. So, in any particular instantiation of your
    > 'Array' template, 'element_type' will mean something in particular.
    >
    >> I want to understand the scope operatorn in such way that there is some
    >> variable
    >> in the Array template class that has name element_type.

    >
    > I don't understand the sentence above.
    >
    >> The fully name of the Array template class when you have int as the
    >> parameter is
    >> Array<int>

    >
    > It's called "instantiation", not "fully name of the Array template class".
    >
    >> Can you give me some more explanation about Array<int>::element_type
    >> that might help me to understand this.

    >
    > See above. And, yes, I can give you more if you ask more specific
    > questions.
    >
    > V


    I think I start to understand it now. I just check with you Victor hope you
    read this.
    Assume there exist a valid type x that I use to get a specialisation of the
    template Array.
    If you instantiate the template class Array and pass type x as parameter you
    get the instantiation name Array<x>.
    If I want to access a class-wide member in the Array template I use the ::
    In my case I have a class-wide member with name element_type so I access
    this element_type by giving statement
    Array<x>::element_type
    this whole expession can always be substituted with the type that this
    template was called with in my example here it's x because we have this
    typedef T element_type

    Does my thinking seems to be about correct?

    //Tony
     
    Tony Johansson, Aug 15, 2005
    #5
  6. Tony Johansson wrote:
    > [..]
    > Assume there exist a valid type x that I use to get a specialisation of the
    > template Array.
    > If you instantiate the template class Array and pass type x as parameter you
    > get the instantiation name Array<x>.
    > If I want to access a class-wide member in the Array template I use the ::
    > In my case I have a class-wide member with name element_type so I access
    > this element_type by giving statement
    > Array<x>::element_type
    > this whole expession can always be substituted with the type that this
    > template was called with in my example here it's x because we have this
    > typedef T element_type
    >
    > Does my thinking seems to be about correct?


    Yes... I am trying to find some kind of hidden pothole in that or
    some additional condition that has to be satisfied, but it all seems
    to be fine.

    I recommend looking at the standard library containers and how they
    define 'value_type' members. That's a common requirement for all of
    them, and you should be able to rely on that, for example.

    V
     
    Victor Bazarov, Aug 15, 2005
    #6
  7. Tony Johansson

    neerajk Guest

    "Array<int>::element_type" is really "typename
    Array<int>::element_type"

    and in fact the newer compilers issue warnings asking you to write it
    this way.

    It is a hint to the compiler to look for a type definition rather than
    a data/function member.
     
    neerajk, Aug 16, 2005
    #7
  8. neerajk wrote:
    > "Array<int>::element_type" is really "typename
    > Array<int>::element_type"
    >
    > and in fact the newer compilers issue warnings asking you to write it
    > this way.


    Really? Which ones? Please post a complete compilable piece of code
    that illustrates what you're talking about.

    V
     
    Victor Bazarov, Aug 16, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Nielsen
    Replies:
    3
    Views:
    428
  2. oor
    Replies:
    0
    Views:
    1,411
  3. Replies:
    1
    Views:
    627
  4. Steven D'Aprano
    Replies:
    0
    Views:
    143
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    116
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page