C ++ factory function with boost :: noncopyable

Suppose I need to implement a factory function that returns an O object that inherits / has members inheriting from boost :: noncopyable.

struct O : boost::noncopyable {};
O factory() { return O(); }

Obviously, the return value will not compile.

What method do you know or use to implement such factory methods? I really like to avoid overriding the copy constructor, if possible, and return a value, not a link or pointer.

after some mockery and a link with codeka, I succeeded (I don’t know how portable it works, it 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);
}
+3
source share
2 answers

Boost BOOST_MOVABLE_BUT_NOT_COPYABLE, , , , ( ++ 11) () ...

+2

iscopcopyable, . .

, , , , , , , . , , , , ?

+7

Source: https://habr.com/ru/post/1752772/


All Articles