0

Assuming we have an implementation of a template class for a matrix. I wish to represent the matrix by a std::vector of type T.

Now, I wish to return an iterator of the matrix by returning the vector's iterator. For that, I saw that using the keyword typename is required in the public section:

typedef typename std::vector<T>::iterator iterator.

First, I don't understand why typename is required here.

Second, everyone can see that I use a std::vector to represent the matrix. What can I do to prevent that information exposing.

How can I achieve this goal without exposing the matrix representation?

Alex Goft
  • 1,114
  • 1
  • 11
  • 23
  • For your first question: http://stackoverflow.com/questions/7923369/when-is-the-typename-keyword-necessary – Mat Sep 06 '15 at 14:34

3 Answers3

0

First, i dont understand why typename included here.

typename tells the compiler that iterator is indeed a type, rather than a member, for example, which would look exactly the same. You need to qualify this when dealing with templates, because in general at this point the compiler wouldn't have enough information to tell the difference.

Second, everyone can see that i use a vector to represent the Matrix, What can i do to prevent that information exposing.

While everyone can see that you are using a vector, it is not a trade secret. You provide a typedef for the outside world to use, emphasising that a vector is just an implementation detail, and if they don't, they deserve whatever comes, in my opinion.

To emphasise this, vector<T>::iterator is also a typedef, and in some of the compilers it means different things when you compile with different flags. Sometimes you could look it up and use T* everywhere, but when the code eventually breaks, would you really complain on the developers of standard library?

I don't think you could hide this any better: the compiler needs to know what the iterator is.

Maksim Solovjov
  • 3,147
  • 18
  • 28
0

First off you need the typename there as you are using a dependent type. for more information on that see: Where and why do I have to put the “template” and “typename” keywords?

For the second part you can do two typedefs. One in the private section that is

typedef typename std::vector<T> container;

And then in the public section you could do

typedef typename container::iterator iterator;

And you can see how that compiles here

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

You need to include typename here because there is a inferred type, T, present. This has been asked and answered here.

For your second question, you're not really exposing the class representation here; the user of your class does not need to understand that the underlying Matrix representation is a std::vector in order to use your class. The information would actually only be available if the user went to some effort to look for it (e.g. via a compilation error). Even if you defined your own iterator, the user could still just look at your source code.

Finally, if you are using C++11, a better way of doing the same thing is to use aliases:

using iterator = typename std::vector<T>::iterator;

No immediate advantage in this case, but good to get into this habit.

Community
  • 1
  • 1
Daniel
  • 8,179
  • 6
  • 31
  • 56