By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

special template specialisation

P: n/a
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/]______\/
Mar 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2007-03-21 18:30, Adrian Hawryluk wrote:
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[i]) {}
};

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
Mar 21 '07 #2

P: n/a
Erik Wikström wrote:
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[i]) {}
};

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/]______\/
Mar 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.