473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Is copy constructible



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 ?

Jul 19 '05 #1
3 2631
"Gianni Mariani" <gi*******@mariani.ws> wrote...
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

Jul 19 '05 #2
Victor Bazarov wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote...
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?


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".

Jul 19 '05 #3
"Gianni Mariani" <gi*******@mariani.ws> wrote...
Victor Bazarov wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote...
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?


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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
7
by: Jan | last post by:
Hi There, I want to insert an object with private copy constructor at the and of a STL list. Is there an easy way to do it like that: list <x> myList; // x has private copy constructor .... ...
24
by: rdc02271 | last post by:
Hello! Is this too crazy or not? Copy constructor: why can't I copy objects as if they were structs? I have a set of simple objects (no string properties, just integers, doubles) and I have to...
7
by: responsible | last post by:
Hi, Assume I have the following class class test{ public: int x; test(){ x = 5; } private:
1
by: better_cs_now | last post by:
Hello all, In this code: class Bar { public: Bar(int) {} // ***Not default constructible*** }; class Foo {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.