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

Removing Templates from Code - Best Method

P: n/a
Hi All,

This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:

I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.

In a nutshell what is the best way to take code with templates and
turn it into code without templates?

Thanks in advance,

Chris
Aug 6 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Aug 6, 8:00*am, Chris <christopherri...@gmail.comwrote:
Hi All,

This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:

I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. *I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.

In a nutshell what is the best way to take code with templates and
turn it into code without templates?

Thanks in advance,

Chris
Hi Chris,

Templates are skeletons for classes (speaking of class templates).
They need to be instantiated with type information to actually become
a class as we understand. Therefore if I were you I would check
template instantiations in the code, if they are multiple (the same
skeleton instantiated with and integer and a float in class template
with single type information for example), then you will experience a
problem. You might need to rewrite several parts of the code depending
on what kind of information relevant to these types are used. If size
information is used in members to do computations and instantiation
types for the template have different sizes then you will need to
introduce some logic to determine type (or use run time type
information techniques, if you can in your case). If this does not
work you may need to inevitably write separate classes for different
type instantiations.
Same arguments apply to function templates. A quick way to get rid of
type dependency in function arguments is to replace the template
one(s) with a void* and modify the caller places. Although beware
again that you may need to introduce size related arguments to your
original function, if that is needed inside the function.

Have a nice day
Baris
Aug 6 '08 #2

P: n/a
On Aug 5, 10:54*pm, barış can kaşıkçı <bariskasi...@gmail.comwrote:
On Aug 6, 8:00*am, Chris <christopherri...@gmail.comwrote:
Hi All,
This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:
I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. *I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.
In a nutshell what is the best way to take code with templates and
turn it into code without templates?
Thanks in advance,
Chris

Hi Chris,

Templates are skeletons for classes (speaking of class templates).
They need to be instantiated with type information to actually become
a class as we understand. Therefore if I were you I would check
template instantiations in the code, if they are multiple (the same
skeleton instantiated with and integer and a float in class template
with single type information for example), then you will experience a
problem. You might need to rewrite several parts of the code depending
on what kind of information relevant to these types are used. If size
information is used in members to do computations and instantiation
types for the template have different sizes then you will need to
introduce some logic to determine type (or use run time type
information techniques, if you can in your case). If this does not
work you may need to inevitably write separate classes for different
type instantiations.
Same arguments apply to function templates. A quick way to get rid of
type dependency in function arguments is to replace the template
one(s) with a void* and modify the caller places. Although beware
again that you may need to introduce size related arguments to your
original function, if that is needed inside the function.

Have a nice day
Baris
Thank You Very Much Baris, this gives me something very concrete to go
on... I think I'm going to have to end up rewriting some code but the
majority I may get lucky and be able to use void*.

Again Great Description Most Appreciated,

Chris :)
Aug 6 '08 #3

P: n/a
M.T
Chris <ch**************@gmail.comwrites:
I think you can write a script to do such works. using regex,you can
match string like this
>template[\n\t]*<*>*\(*\)(const)?{something}
And then delete it.Surely the regex expression above will not work.
maybe you can learn to using regex expressions to solve problems.
Hi All,

This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:

I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.

In a nutshell what is the best way to take code with templates and
turn it into code without templates?

Thanks in advance,

Chris
Aug 6 '08 #4

P: n/a
Chris <ch**************@gmail.comwrites:
This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:

I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.

In a nutshell what is the best way to take code with templates and
turn it into code without templates?
That would depend on the template.
I think we can identify different kinds of templates.

Some are purely meta-programming (probably the hardest to "translate").

Some implement syntactic abstraction; they could possibly be
translated as macros.

Some are used for true generic programming. These ones will be the
most agreable to translate into Objective-C. You will be able to
implement them as Objective-C classes using objects ids instead of
template parameters, and sending our beloved dynamic messages instead
of generic ->method calls.

And of course, you will have to distinguish class templates from
function template, from member templates inside class (templates or
not). Class and member templates you will probably be able to
translate as normal Objective-C classes and methods (with the template
parameters taken as normal generic parameters such as id or SEL). For
function templates, you could implement them as a methods in a
category to some class up in the hierarchy (perhaps NSObject), or as
plain C function (with generic parameters).

--
__Pascal Bourguignon__
Aug 6 '08 #5

P: n/a
On Aug 6, 7:00*am, Chris <christopherri...@gmail.comwrote:
Hi All,

This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:

I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. *I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.
Did you know that if you ended your objective-c file with .mm instead
of .m
you can use C++ constructions.... Don't know if it solves your
problem, but
it might be the easy way :)

Good luck,
Bas
Aug 6 '08 #6

P: n/a
On Aug 6, 11:18 am, Chris <christopherri...@gmail.comwrote:
I have to move some code from c++ to objective-c and to do this I must
remove all defined templates.
If it helps you, I successfully tested some basic template code using
Objective-C++ over 1.5 years ago. It worked just fine though I faintly
remember that the manual mentioned certain restrictions regarding the C
++ idioms you can/can't use in objective-C++. Please refer to your
manual and you might find a better way than translating code.

regards,
Aman Angrish.
Aug 6 '08 #7

P: n/a
Chris wrote:
Hi All,

This is a weird one but I am hoping someone can help or has some
pointers, a recipe how to do the following:

I have to move some code from c++ to objective-c and to do this I must
remove all defined templates. I am not really a c++ guy so I have not
worked with templates all that much (not really at all) and as such I
don't have a good idea where to start.

In a nutshell what is the best way to take code with templates and
turn it into code without templates?

Thanks in advance,

Chris
See this discusssion:
http://www.velocityreviews.com/forum...ont-from-.html

They say, Comeau has some facility to translate C++ to C (which then
will probably be not difficult to compile with Objective C). Cfront used
to be the guy, but I just found out it has been discontinued long ago so
it probably will not translate your C++ code if it is more or less new.
Otherwise, you can check out
http://www.softwarepreservation.org/...us_plus/cfront.

Hope this will help
-Pavel
Aug 8 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.