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?