1

Given this code

template <typename T>
typename T::ElementT at (T const &a , T const &b)
{
        return a[i] ;
}

what do

typename T::ElementT 

and

a[i]

mean?

sbi
  • 204,536
  • 44
  • 236
  • 426
user722528
  • 579
  • 5
  • 11
  • May I ask where you got this piece of code from? It seems quite useless? And did it have those errors (I think it should be `const typename T::ElementT`, and what's with `b` vs. `i`?) or did you put them in? – sbi Apr 02 '11 at 07:18

3 Answers3

3
typename T::ElementT 

Since T:ElementT is a dependent name, that is why you see the keyword typename before it. It tells the compiler that ElementT is a tested type, not value.

And as for a[i], it seems that T is a class that has defined operator[] which is being called when you write a[i]. For example, T could be sample as (partially) defined here:

class sample
{
 public:
      typedef int ElementT; //nested type!

      //...

      ElementT operator[](int i) 
      {
          return m_data[i];
      }

      ElementT *m_data;
      //...
};

Now, if T is sample, then you can write T::ElementT as well as a[i] which is of type T. In this case when T is sample, I'm assuming that the type of index i is int.

Nawaz
  • 327,095
  • 105
  • 629
  • 812
0

i guess in that code T is always a class which has operator [] overloaded and has a subclass defenition ElementT any other class which doesn't have these two qualities will make an error while compile.

Ali1S232
  • 3,295
  • 1
  • 24
  • 42
0
typename T::ElementT 

This is explained exhaustingly by Johannes in this entry to the C++ FAQ.

a[i]

This operation is usually called "subscription" and accesses the i-th element in a. In order to do that, a must either be an array or some class that overloads the subscription operator (like std::vector or std::map).
However, as Nawaz pointed out in his comment, since a is of type T, and since T is expected to have a nested type ElementAt, in this case a cannot be an array.

Community
  • 1
  • 1
sbi
  • 204,536
  • 44
  • 236
  • 426
  • in this case, `a` cannot be an array, because the type of `a` is `T` which has a nested type! – Nawaz Apr 02 '11 at 07:19