|
Boost Users : |
Subject: [Boost-users] Using Adaptable Generator concept with generate_n and boost smart pointers
From: Marco Craveiro (marco.craveiro_at_[hidden])
Date: 2012-10-18 19:43:11
Hi boosters,
I'm trying to create a class that models the concept of adaptable
generator [1]. The idea is that it generates pointers to test data
objects which I'd like to then put inside of smart pointers such as
shared_ptr or scoped_ptr (or even std::shared_ptr) via generate_n,
etc. This is a (non-compilable) example of my intentions:
#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>
#include <iterator>
struct base { int i; };
struct derived : public base { int z; };
struct generator
{
typedef base result_type;
result_type* operator()() {
return new derived();
}
};
int main(void) {
std::vector<std::shared_ptr<base> > z;
std::generate_n(std::back_inserter(z), 5, generator());
return 0;
}
The std::shared_ptr is incidental; it could have been
boost::shared_ptr, etc - its up to client code to decide. I get this
to work fine when result_type is say boost::shared_ptr<base> but what
I really like is for it to be a raw pointer; is there some boost magic
(or standard C++ 11 magic) I can apply to convert it to the value_type
of the container I'm trying to generate?
Many thanks for your time,
Marco
-- So young, and already so unknown -- Pauli blog: http://mcraveiro.blogspot.com [1] http://www.sgi.com/tech/stl/AdaptableGenerator.html
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net