J
jeroen.haegebaert
I'm wondering whether the following code construct is allowed according
to the C++ standard.
The following code fragment illustrates the situation:
#include <vector>
using namespace std;
// Class A has a member v of type std::vector<int>
class A
{
public :
std::vector<int> v;
};
// class B stores an iterator (i) to such a vector:
class B
{
public :
std::vector<int>::iterator i;
};
int main()
{
A *a = new A();
B b;
b.i = a->v.begin();
// make sure a is deleted before b goes out of scope
delete a;
}
We only found this to be a problem when compiling with the Microsoft
compiler using cl /EHsc /MTd /D_HAS_ITERATOR_DEBUGGING#0. In that case,
it results in a crash inside the destructor of B::i.
Now my question: does the standard allow storing iterators like that?
thx,
jeroen
to the C++ standard.
The following code fragment illustrates the situation:
#include <vector>
using namespace std;
// Class A has a member v of type std::vector<int>
class A
{
public :
std::vector<int> v;
};
// class B stores an iterator (i) to such a vector:
class B
{
public :
std::vector<int>::iterator i;
};
int main()
{
A *a = new A();
B b;
b.i = a->v.begin();
// make sure a is deleted before b goes out of scope
delete a;
}
We only found this to be a problem when compiling with the Microsoft
compiler using cl /EHsc /MTd /D_HAS_ITERATOR_DEBUGGING#0. In that case,
it results in a crash inside the destructor of B::i.
Now my question: does the standard allow storing iterators like that?
thx,
jeroen