Removing Templates from Code - Best Method

C

Chris

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
 
B

barýþ can kaþýkçý

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
 
C

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 :)
 
M

M.T

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.
 
P

Pascal J. Bourguignon

Chris said:
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).
 
C

Colander

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
 
A

aman.c++

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.
 
P

Pavel

Chris said:
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/forums/t287919-where-can-i-download-cfront-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/projects/c_plus_plus/cfront.

Hope this will help
-Pavel
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top