From: Peng Yu on
Suppose I have an std::vector<int>::iterator that I get from somewhere
else. I'm not sure if it is properly initialized. Is there a way to
test in runtime?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Peng Yu wrote:
> Suppose I have an std::vector<int>::iterator that I get from somewhere
> else. I'm not sure if it is properly initialized. Is there a way to
> test in runtime?

No. You are expected to write your code in a way that invalid iterators
don't happen, everything else is just "undefined behaviour". That said,
many major implementations provide a mode that sacrifices complexity
guarantees and efficiency for additional checks, which you can use for
debugging.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 15 Jun., 11:16, Peng Yu <pengyu...(a)gmail.com> wrote:
> Suppose I have an std::vector<int>::iterator that I get from somewhere
> else. I'm not sure if it is properly initialized. Is there a way to
> test in runtime?

There does not exist a general test function for this. This
is so, because pointers are iterators and there also exists
no general/portable mechanism to verify whether a pointer
value is valid:

int main() {
int* p, q; // not initialized
if (p == q) ; // Undefined behaviour
}

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jens Schmidt on
Peng Yu wrote:

> Suppose I have an std::vector<int>::iterator that I get from somewhere
> else. I'm not sure if it is properly initialized. Is there a way to
> test in runtime?

No, you can't test that.
It is bad design to use uninitialized iterators in C++, the same as
with any other uninitialized data.
Someone giving you this iterator will get what he/she deserves:
unspecified behaviour.
--
Greetings,
Jens Schmidt


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Leigh Johnston on
"Peng Yu" <pengyu.ut(a)gmail.com> wrote in message
news:6680bd0a-33c3-4f0a-aa5d-6c65959cd5db(a)k39g2000yqb.googlegroups.com...
> Suppose I have an std::vector<int>::iterator that I get from somewhere
> else. I'm not sure if it is properly initialized. Is there a way to
> test in runtime?
>

The only valid operations on an invalid iterator are initialization and
assignment so the answer to your question is "no".

/Leigh


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]