Is copy constructible

G

Gianni Mariani

I'm developing a factory template that supports placement new and one of
the methods I need to provide is a copy constructor. The question is,
how do I make the template generic for implementations that may not
support the copy constructor.


It would be very interesting to be able to write:


template <typename T, bool>
struct copy_constructor
{

virtual T * creator( T& v )
{
throw "No copy constructor";
}
};

template <typename T>
struct copy_constructor<T, true>
{

virtual T * creator( T& v )
{
return new T( v );
}

};


template <typename T>
struct factory : copy_constructor<T,is_copy_constructible<T> >
{

virtual T * creator()
{
return new T;
}
};



I'm not even sure it's the right way to do this yet. It's more a
question of "can it be done". I looked at boost::type_traits.hpp
without finding anything suitable.

ideas ?
 
V

Victor Bazarov

Gianni Mariani said:
I'm developing a factory template that supports placement new and one of
the methods I need to provide is a copy constructor. The question is,
how do I make the template generic for implementations that may not
support the copy constructor.

As you will be able to tell from this response, I don't exactly understand
why you need to dance around this. Just document the fact that the type
used with your template has to be copy-constructible and that's all.

A type may be intentionally made _not_ copy-constructible. If you allow
to use your factory that will work around that, you're circumventing the
intention of the designer of that type, no?

Victor
 
G

Gianni Mariani

Victor said:
As you will be able to tell from this response, I don't exactly understand
why you need to dance around this. Just document the fact that the type
used with your template has to be copy-constructible and that's all.

A type may be intentionally made _not_ copy-constructible. If you allow
to use your factory that will work around that, you're circumventing the
intention of the designer of that type, no?

Well, it's a "generic" factory object that we might want a placement
copy constructor method or not depending on if the type is copy
constuctible.

The factories are placed in a registry which contain all factories for
implementations that have the factory interface - wether they are copy
constructible or not.

The design is a generic interface to *structing - including support for
PIMPL. The issue that arises is what to do for copy constructors.

One possible solution is to leave it up to when the factory is
instantiated and let the implementor specify wether the implementation
is copy constructible. Still, it would be neat if the default was
"natural".
 
V

Victor Bazarov

Gianni Mariani said:
Well, it's a "generic" factory object that we might want a placement
copy constructor method or not depending on if the type is copy
constuctible.

The factories are placed in a registry which contain all factories for
implementations that have the factory interface - wether they are copy
constructible or not.

The design is a generic interface to *structing - including support for
PIMPL. The issue that arises is what to do for copy constructors.

One possible solution is to leave it up to when the factory is
instantiated and let the implementor specify wether the implementation
is copy constructible. Still, it would be neat if the default was
"natural".

Ah... Well, it sounds like you're trying to create something a bit
too generic (again, it's possible that I am not fully appreciating
the possibilities such a solution provides).

I suppose that due to my incomplete comprehension of the problem you
are trying to solve I cannot see a clear solution for it. Sorry to
have wasted your time and bandwidth.

Speaking of placement and factories, it does sound a bit like you're
trying to produce a memory management mechanism. What if you looked
at the existing ones? I cannot claim any deep knowledge of those on
the market, but maybe a search on Google could help you find what is
out there.

In any case, best of luck!

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top