Reputation: 677
Assuming code that looks vaguely like this:
bool LoadGame() {
vector<string> SaveFile = LoadFromSaveFile();
Data = SaveFile.begin();
LoadCharacters(&Data);
// containing LoadOneCharacter(&Data) calls
LoadLocations(&Data);
// >> LoadOneLocation(&Data) calls
// etc.
}
Is there any way to check whether Data is currently pointing at SaveFile::end() while within LoadCharacters(), LoadLocations(), etc., without passing SaveFile::end() to all the functions invoked by LoadGame()?
(If only dereferencing end() and beyond threw an exception instead of generating undefined behavior!)
Upvotes: 3
Views: 203
Reputation: 392911
This is not possible, in the general case
In specific cases, you may be able to exploit implementation specific details of the iterator implementation to get at the container.
The iterators of the standard container library have no such details documented AFAIR.
A special case would be input iterators like std::istream_iterator<>
which can always be compared to default-constructed instance of itself to check for end-of-input.
std::istringstream iss("test");
std::istream_iterator<char> it(iss);
// you can always check for end of input:
while (std::istream_iterator<char>() != it)
{
it++;
}
Upvotes: 5
Reputation: 73480
It's not possible in general. In many implementations many kinds of iterators are just pointers, and have no understanding of the underlying container.
You'll need to pass both iterators, or pass a different structure such as a range
( which is in boost
but not C++0x IIRC).
Upvotes: 5