Well, first off, I don't know why decltype isn't working. I get the error C2327: 'Foo<T>::container' : is not a type name, static, or enumerator, suggesting something, but I'm just not going to try figuring it out.
Secondly...
Originally Posted by
StainedBlue
I want to take advantage of how iterators are really pointers underneath it all. If i sort the list, the iterators go with the elements they point to. And so if the wrapper objects contain the original container values, plus an added iterator pointer, i should be able to achieve O(1) efficiency of getting an iterator into any of the projected containers, at the desired location. That's the idea anyway.
...I am uncertain of this. I don't know if sort invalidates the iterators, but I do believe it should. A iterator is a pointer to a certain element, not just a glorified pointer. So I'd say you're invoking undefined behavior.
But just for giggles, I'll C++0x-ify your code:
Code:
#include <iostream>
#include <algorithm>
#include <list>
int main()
{
std::list<int> base;
std::list<decltype(base.begin())> base_iterators;
typedef decltype(*base.begin()) base_t;
for( int i = 0; i < 10; i++)
base.push_back(i);
for(auto it = base.begin(); it != base.end(); it++)
base_iterators.push_back(it);
std::list<int>::iterator it_ptr = base.begin();
std::cout << "Beginning: " << *it_ptr << "\n\n";
std::for_each(base.begin(), base.end(), [](base_t i) { std::cout << i << "\n"; });
base.sort([](base_t a, base_t b) { return a > b; });
std::cout << "\nEnd: " << *it_ptr << "\n\n";
std::for_each(base.begin(), base.end(), [](base_t i) { std::cout << i << "\n"; });
std::cout << "\nIterating backwards from the end:\n\n";
std::list<int>::iterator it_copy = it_ptr;
for (int i = 0; i < 3; i++, it_copy--)
std::cout << *it_copy << "\n";
return 0;
}
Also, is this going to work? I don't know.
You made a fatal flaw when creating the code. You were binding a pointer to a temporary. That's illegal. Your compiler should have picked up that.
Basically, it was the line
Code:
std::list<int>::iterator* it_ptr = &base.begin();
begin() returns a temporary and you're creating a pointer to it. You're lucky it works or worked.
You need to create an iterator to begin first, then you can create a pointer to it.