Is there any way I can pass vector's index to an constructor of it's element? for example:
#include <iostream>
#include <vector>
class Foo {
public:
Foo(unsigned long index) {
std::cout << index << std::endl;
}
};
int main() {
std::vector<Foo> foo;
foo.resize(2); // any way to make this work?
}
this code does of cause not work because the compiler don't know how to construct a Foo(unsigned long index), but any way I can do some tricks(for example to custom an allocator?) to make this code actually work?
Foois only constructible with a provided ctor-argument. I'm curious, however, what is the real problem this solution is attempting to solve, because chances are there lays a more proper solution to that, than trying to make this work.Foo's come from, and how they are managed (are there more than onefoofloating around in various places? canFoo's be moved from onefooto another? what are the expectations when aFoois removed from afooand thereafter all subsequent indices no longer align?), an alternative identity-mapping may be worth considering. Anyway, thanks for clarifying.