special template specialisation

A

Adrian Hawryluk

Does anyone know if there is a way of specialising based on whether
there is a constructor or destructor defined (i.e. not the default
constructor/destructor that C++ creates)?

I would like to try and optimise a template class's initialisation to
not bother initialising its elements if the type contained has no
specified default constructor. The same holds for the destructor.

Of course, the compiler may be able to optimise this, but I'm not sure
how far an optimiser will go in this case.

For efficiency reasons, I do not state the type in the class, but
allocate enough space for the type and use an in-place new on
construction, with a corresponding call to each destructor on
destruction. However, if there are many elements, the initialisation /
deinitialisation of all of the elements would be a waste of time if
there was actually nothing done. Could an optimiser detect this and
skip initialisation altogether?

Thanks in advance,


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Does anyone know if there is a way of specialising based on whether
there is a constructor or destructor defined (i.e. not the default
constructor/destructor that C++ creates)?

I would like to try and optimise a template class's initialisation to
not bother initialising its elements if the type contained has no
specified default constructor. The same holds for the destructor.

Of course, the compiler may be able to optimise this, but I'm not sure
how far an optimiser will go in this case.

For efficiency reasons, I do not state the type in the class, but
allocate enough space for the type and use an in-place new on
construction, with a corresponding call to each destructor on
destruction. However, if there are many elements, the initialisation /
deinitialisation of all of the elements would be a waste of time if
there was actually nothing done. Could an optimiser detect this and
skip initialisation altogether?

In general I believe that to be a bad idea (if I understand you
correctly, not sure I do). I'm not sure but I think it could cause some
damage where members are not initialized correctly, consider this:
class Foo
{
int* data;
public:
Foo(size_t i = 5) : data(new int) {}
};

class Bar
{
Foo foo;
};

Now, if you should use your idea for creating a collection of Bar-
objects (I assume a collection since you are concerned with the time
needed to initialize each object) but don't run Bar's constructor
whouldn't that mean that Foo's constructor is run either? So even though
Bar's constructor does nothing it think that it needs to be run to run
the constructors of each of it's members.

-- Erik Wikström
 
A

Adrian Hawryluk

Erik said:
In general I believe that to be a bad idea (if I understand you
correctly, not sure I do). I'm not sure but I think it could cause some
damage where members are not initialized correctly, consider this:
class Foo
{
int* data;
public:
Foo(size_t i = 5) : data(new int) {}
};

class Bar
{
Foo foo;
};

Now, if you should use your idea for creating a collection of Bar-
objects (I assume a collection since you are concerned with the time
needed to initialize each object) but don't run Bar's constructor
whouldn't that mean that Foo's constructor is run either? So even though
Bar's constructor does nothing it think that it needs to be run to run
the constructors of each of it's members.


Point taken. I'm going to have to leave it up to the optimiser then.

Thanks Erik,


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top