#include <iostream>
#include <vector>
template <typename Container>
void f(Container& v)
{
std::cout << "f() v.data() " << v.data() << ", v.size() " << v.size() << '\n';
for (int& n : v) n += 10;
}
void g(std::vector<int>& v)
{
std::cout << "g() v.data() " << v.data() << ", v.size() " << v.size() << '\n';
for (int& n : v) n += 100;
}
int* p_;
struct My_alloc : std::allocator<int>
{
My_alloc(int* p) { p_ = p; }// : p_(p) { }
template <class U, class... Args>
void construct(U* p, Args&&... args) { std::cout << "My_alloc::construct(U* " << p << ")\n"; }
template <class U> void destroy(U* p) { std::cout << "My_alloc::destroy(U* " << p << ")\n"; }
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0)
{
std::cout << "My_alloc::allocate() return " << p_ << "\n";
return p_;
}
void deallocate(pointer p, size_type n) { std::cout << "deallocate\n"; }
template <typename U>
struct rebind { typedef My_alloc other; };
// int* p_;
};
int main()
{
std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << "main() v.data() " << v.data() << '\n';
My_alloc my_alloc(&v[3]);
std::vector<int, My_alloc> w(3, my_alloc);
f(w);
g(reinterpret_cast<std::vector<int>&>(w));
for (int n : v) std::cout << n << ' ';
std::cout << '\n';
std::cout << "sizeof v " << sizeof v << ", sizeof w " << sizeof w << '\n';
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdGVtcGxhdGUgPHR5cGVuYW1lIENvbnRhaW5lcj4Kdm9pZCBmKENvbnRhaW5lciYgdikKewoJc3RkOjpjb3V0IDw8ICJmKCkgdi5kYXRhKCkgIiA8PCB2LmRhdGEoKSA8PCAiLCB2LnNpemUoKSAiIDw8IHYuc2l6ZSgpIDw8ICdcbic7CiAgICBmb3IgKGludCYgbiA6IHYpIG4gKz0gMTA7Cn0KCnZvaWQgZyhzdGQ6OnZlY3RvcjxpbnQ+JiB2KQp7CglzdGQ6OmNvdXQgPDwgImcoKSB2LmRhdGEoKSAiIDw8IHYuZGF0YSgpIDw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