views:

2312

answers:

9

In particular, would it be possible to have code similar to this c++ code executed at compile time in c#?

template <int N>
struct Factorial 
{
    enum { value = N * Factorial<N - 1>::value };
};

template <>
struct Factorial<0> 
{
    enum { value = 1 };
};

// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}
A: 

No. (more text to meet minimum answer length)

Brian
why downvote, that was funny!
GogaRieger
A: 

To a very limited extent, C# something that could be interpreted as meta-programming. But really it's nothing more than overload resolution. It's a real stretch to call it meta programming.

Example:

static string SomeFunc<T>(T value) {
    return "Generic";
}
static string SomeFunc(int value) {
    return "Non-Generic";
}

static void Example() {
    SomeFunc(42);           // Non-Generic
    SomeFunc((object)42);   // Generic
}
JaredPar
I love getting voted down with no reason or comment.
JaredPar
The questioner hasn't really specified what they mean by Metaprogramming, but I think it's safe to say that it's different to what you mean.
Anthony
@Anthony, the problem with ambiguous questions is they produce incorrect or not-intented answers. I could not answer the question or provide an answer which may answer the question. As long as I'm here, I figure I might as well answer :)
JaredPar
+5  A: 

sort of look for t4 templates

Sorry on iphone and can't point to resource.

Scott Hansleman posted last week about it.

This is exactly why SO needs an special iPhone version.
Lucas McCoy
+11  A: 

No, metaprogramming of this complexity is not supported directly by the C# language. However, like @littlegeek said, the Text Template Transformation Toolkit included with Visual Studio will allow you to achieve code generation of any complexity.

(More from Scott Hanselman)

Jacob
no problem littlegeek; i voted you up for beating me by 4 hours even without a Clipboard :)
Jacob
Very cool, thanks.
Brian R. Bondy
+2  A: 

The essential difference between .NET Generics and C++ Templates is that generics are specialized at runtime. Templates are expanded at compile time. The dynamic behavior of generics makes things like Linq, expression trees, Type.MakeGenericType(), language independence and code re-use possible.

But there is a price, you can't for example use operators on values of the generic type argument. You can't write a std::complex class in C#. And no compile-time metaprogramming.

Hans Passant
I know by experience that template code can be reused. And the so-called "language independence" you're referring to actually means you have to use a .net compliant language...There are limitations to both .net generics as well as c++ templates. But these are not the correct ones.
Benoît
+5  A: 

You must be carefull when talking about compile-time when dealing with Java or .Net languages. In those languages you can perform more powerfull metaprogamming (in the broader sense - reflection- ) than C++ due to the fact that "compilation time" (JIT) can be postponed after "run time" ;)

thAAAnos
+4  A: 

It is going to be possible. Watch Anders Hejlsberg's The Future of C# talk.

Comptrol
+2  A: 

Most people insist on trying to metaprogram from inside their favorite language. That doesn't work if the language doesn't support metaprogramming well; other answers have observed that C# does not.

A way around this is to do metaprogramming from outside the language, using program transformation tools. Such tools can parse source code, and carry out arbitrary transformations on it (that's what metaprogramming does anyway) and then spit out the revised program.

If you have a general purpose program transformation system, that can parse arbitrary languages, you can then do metaprogramming on/with whatever language you like. See DMS Software Reengineering Toolkit for such a tool, that have robust front ends for C, C++, Java, C#, COBOL, PHP and a number of other programming langauges, and has been used for metaprogramming on all of these.

DMS succeeds because it provides a regular method and support infrastructure for complete access to the program structure as ASTs, and in most cases additional data such a symbol tables, type information, control and data flow analysis, all necessary to do sophisticated program manipulation.

EDIT (in response to comment): One could apply DMS to implement the OP's task on C#.

Ira Baxter
-1: Good answer, but it doesn't answer this particular question.
John Saunders
@Saunders: Actually it does, but you have do some homework. If you want to implement the OP's specific task, you'll need to implement constant-folding program transformations and focus them on calls to your Fibonacci function. I note that you dinged my answer, when none of the answers addressed the OP's specific question either.
Ira Baxter
thanks, you pointed me in the right direction for something Im working on!
Ralph Willgoss
+2  A: 

try taking a look at script.net; more info at --http://www.orbifold.net/default/?p=2457