 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mlimber Guest
|
Posted: Thu Sep 15, 2005 9:15 am Post subject: Template code bloat? |
|
|
Consider this (simplified) code:
enum CmdID{ CMD_1, CMD_2 /* ... */ };
template <CmdID id>
struct CmdIDHolder
{
static CmdID GetCmdID() { return id; }
};
template<CmdID id>
struct Cmd : public CmdIDHolder<id>
{
// In my real code, Cmd is an abstract base class that
// does not use id directly; it always calls GetCmdID().
};
struct SomeCmd : public Cmd<CMD_1> {};
struct SomeOtherCmd : public Cmd<CMD_2> {};
// Simplified usage of the above
const CmdID cmdID = SomeCmd::GetCmdID();
CmdIDHolder::GetCmdID() must be static because I use it to register
with an abstract Cmd factory (like the one presented in _Modern C++
Design_, chapter . In the presence of a multiplicity of Cmd
subclasses akin to SomeCmd and SomeOtherCmd, will the templatization
lead to bloated code? That is, will the data members and methods of Cmd
itself be instantiated separately for each CmdID, or is it clear that
that is not necessary because the parameter is really only passed to
the base class? (I'm working on an embedded system with limited code
space, so code-bloatedness is a potential issue.)
If this scenario could lead to bloated code, can you suggest an
alternate approach that would accomplish the same task without much
bloating?
Cheers! --M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nemanja Trifunovic Guest
|
Posted: Thu Sep 15, 2005 1:54 pm Post subject: Re: Template code bloat? |
|
|
In general, most modern compilers would optimize away such code bloat,
but the only way to know for sure is to try it for yourself and check
the generated machine code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Thu Sep 15, 2005 1:55 pm Post subject: Re: Template code bloat? |
|
|
mlimber wrote:
| Quote: | Consider this (simplified) code:
enum CmdID{ CMD_1, CMD_2 /* ... */ };
template <CmdID id
struct CmdIDHolder
{
static CmdID GetCmdID() { return id; }
};
template
struct Cmd : public CmdIDHolder
{
// In my real code, Cmd is an abstract base class that
// does not use id directly; it always calls GetCmdID().
};
struct SomeCmd : public Cmd
struct SomeOtherCmd : public Cmd<CMD_2> {};
[snip] |
Upon reflection, this code won't do what I want because I need to refer
to SomeCmd and SomeOtherCmd instances with a generic Cmd pointer.
This seems to be what I need:
// CmdID and CmdIDHolder as above
struct Cmd
{
// Make CmdID accessible to Cmd class
virtual CmdID GetID() const = 0;
};
struct SomeCmd
: public Cmd
, public CmdIDHolder<CMD_1>
{
CmdID GetID() const { return GetCmdID(); }
};
struct SomeOtherCmd
: public Cmd
, public CmdIDHolder<CMD_2>
{
CmdID GetID() const { return GetCmdID(); }
};
CmdID cmdID = SomeCmd::GetCmdID();
Cheers! --M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|