473,320 Members | 1,722 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,320 software developers and data experts.

special template specialisation

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
2 1280
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
6
by: johnbrown105 | last post by:
Is it possible to force the compiler to use a generic template rather than a matching specialisation? Consider the following: ////////////////////////////////////////// template.cpp starts...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> 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...
0
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...
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
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.