hello.
Suppose I need to implement factory function which returns object O which inherits/has members inheriting from boost::noncopyable.
struct O : boost::noncopyable {};
O factory() { return O(); }
Obviously returning value fails to compile.
What method do you know or use to implement such factory methods? I really like to avoid overriding copy constructor if it is possible and to return value rather than reference or pointer.
after some tinkering and link from codeka I managed this (no idea how portable does this, seems to work with g++):
template<class E>
struct threads_parallel_for_generator
: for_generator<E, threads_parallel_for_generator<E> > {
typedef for_generator<E, threads_parallel_for_generator> base_type;
struct factory_constructor {
explicit factory_constructor(const E &expression)
: expression_(expression) {}
operator const E&() const { return expression_; }
private:
E expression_;
};
threads_parallel_for_generator(const factory_constructor & constructor)
: base_type(constructor, *this) {}
private:
boost::mutex mutex_;
};
template<class E>
static threads_parallel_for_generator<E>
parallel_for(const E &expression) {
typedef threads_parallel_for_generator<E> generator;
return typename generator::factory_constructor(expression);
}
Thanks