Setting pointer to object equal to iter?

Pages: 12
closed account (zb0S216C)
Yes, you have to anyway.

Let's re-iterate my last reply:

The compiler isn't able to deduce the type because std::vector::iterator is a non-deductible context. It is what it is: A context where the compiler is unable to deduce a type from a given expression and/or statement.

iterator and const_iterator are type-definitions within std::vector; these are dependant names; that is, they depend on a template parameters/value. Since the type used in the type-definition of both iterator classes may not be the same underlying type as template-parameter for the std::vector, the compiler cannot assume the two types are related. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
class normal_iterator { };

template <typename T>
class normal_iterator<T const> { };

template <typename T>
class vector
{
    public:
        typedef normal_iterator<T> iterator;
        typedef normal_iterator<T const> const_iterator;
};

In this example, iterator is dependant on the template-parameter T of vector. The compiler could easily deduce the type. However, this is not always the case, and for the compiler to assume iterator and const_iterator depend on the template-parameter T of vector is dangerous.

Wazzak
Last edited on
Oooh I forgot all about assigning variables with functional notation.

How have your constructor initialization lists been going :P

In all seriousness, however, I don't see why you're using a compiler specific function when &(*iter) worked? It's way simpler and more intuitive then some obscure function.
How have your constructor initialization lists been going :P


Well, never really saw the need to them. Book I have seems to think they are, and goes to say...

The constructor initializer is a feature that many reasonably experienced C++ programmers have not mastered.


Which seems weird to me since they seem pretty straight forward. I did get some understanding as to why they are kind of needed though.

Do you mind elaborating a bit as to why they are important? And also why this book says they are hard to master?
closed account (zb0S216C)
ResidentBiscuit wrote:
"Do you mind elaborating a bit as to why they are important? And also why this book says they are hard to master?"

They initialise the data members; any assignments beyond the initialiser list is assignment. It's well known that initialisation is a plus in terms of performance. They aren't hard to master at all - there's nothing to them, really. If you can initialise an object/pointer/reference in either a function or namespace, you can initialise an object/pointer/reference in an initialiser list. The only difficulty you'd encounter with initialiser lists is when you're inheriting, and that's calling the base class' copy-constructor - even that isn't difficult.

Wazzak
Last edited on
Topic archived. No new replies allowed.
Pages: 12