C++ template metaprogramming in C#?

N

Nicholas Paldino [.NET/C# MVP]

Joe,

While template programming isn't available in .NET, something similar is
available. Lookup Generics (in the context of .NET) and you should find a
great deal of information.

There are differences between them, but definitely enough similarities
to qualify for the comparison.
 
J

Joe

Yes. I know Generics. For now I use it to declare typesafe collections
of objects. Then from what you saying I can use it to do the same (or
close) to template metaprogramming in C++ ?
 
B

Ben Voigt [C++ MVP]

Joe said:
Hi,

I found a concept named template metaprogramming that can be used in C+
+ code at compile-time. I am a beginner at C++. But I am a programmer
on the .NET platform. Do you know if template metaprogramming is
supported in C# (.NET)? For reference I found it:
http://en.wikipedia.org/wiki/Template_metaprogramming.

Thanks to all.

No, there is absolutely nothing similar to template metaprogramming in .NET.
Template metaprogramming relies on specializations which generics do not
have.

OTOH, .NET makes it very easy to generate code on the fly via
Reflection.Emit. Performance is not likely to be as good as template
programs run through the C++ compiler (in fact C++ templates are much more
performant than .NET generics even for collection classes, because the C++
compiler will optimize per type and the .NET JIT creates one shared generic
implementation for all reference types).
 
N

Nicholas Paldino [.NET/C# MVP]

Well, you can't do all of the things that you can do with templates in
C++. Parameterizing templates with values other than types, for example, is
not supported.

Is there something specific you are trying to do with templates that you
can't do in C#?
 
J

Joe

I am on a project that will consist of many calculations (processor
intensive). Some people let me know that templates are good for it.
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

My apologies. I saw the "template" in "template metaprogramming" and
missed the real intent of your question completely.
 

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

Top