I hit some behaviour I can’t seem to wrap my head around. Say I have a class defined as follows (a simple example for illustration):
class some_class
{
public:
some_class()
{
x_["0"] = "0";
x_["1"] = "1";
y_[0] = x_.find("0");
y_[1] = x_.find("1");
}
int some_function(int i)
{
if(std::find(y_.begin(), y_.end(), x_.find("1")) != y_.end())
{
std::cout << "Found!" << std::endl;
return 1001;
}
return -1001;
}
private:
std::unordered_map<string, string> x_;
std::array<unordered_map<string, string>::iterator, 2> y_;
};
The code compiles with Visual Studio 2012 RC in debug mode (others modes not tested), but during call to some_function the program fails with a following assertion message
… microsoft visual studio 11.0\vc\include\list Line: 289
Expression: list iterators incompatible…
The calling code looks like this
auto vector<int> as;
as.push_back(1);
auto vector<int> bs;
auto some = some_class();
std::transform(as.begin(), as.end(), std::inserter(bs, bs.begin()), [=](int i) { return some.some_function(i); });
Question:
What would be the problem with this kind of an arrangement? I get the code run fine if I declare x_ and y_ in some_function instead of as member variables. The class is being called/used in std::transform like occasions, if that should factored in to the situation.
As a tangential aside, it looks like the following declaration will be rejected by the compiler, should it be rejected?
std::unordered_map<string, string> x_;
std::array<decltype(x_.begin()), 2> y_;
The error message is
error C2228: left of ‘.begin’ must have class/struct/union
Your situation.
Your
iteratorspoints on elements of anothermap, that stored inthisaftercopy-ctor, and infindthere is comparison.for decltype –
incorrect in
class-block, since there is no object. This will works ifx_will bestatic.