The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

.NET C# to C++

How hard would it be to translate code written in C# to C++?

Back story: I want to switch to .NET for various reasons, but this will lock me to Windows only, which isn't a big deal at the moment, but in case I'd like to go back to C++ for cross-platform development (say on Mac), how hard would be to rewrite the code in C++?

This will be a manual translation. I don't want to buy or create an automatic translator or anything.

My background is in C++. I am new to C#, but it appears that the translation wouldn't be too too hard (apart from any custom GUI elements). A lot of things appear much easier in C# and could potentially be replicated in C++ using STL, Boost, etc.

But I thought I'd ask the crowd here and see if anyone attempted such a thing..
newbie_C#
Thursday, April 17, 2008
 
 
Sounds like a non-starter to me.  You would need to duplicate all the .NET Framework stuff you use in your application.
Joe
Thursday, April 17, 2008
 
 
To give you an idea of the scope of the problem, the easiest way to do it is probably to design a metalanguage and build two compilers for it, one that emits emit C# and one that emits C++.
Robert Rossney Send private email
Thursday, April 17, 2008
 
 
You can look at Mono as an option for cross-platform .NET apps...or Silverlight.

Porting from C# to C++ is a non-starter. If cross platform is a real need and Mono/Silverlight won't be sufficient then pick a different framework than .NET.

But to get a handle on how difficult it is, as a worst case scenario look at Mono. It took them quite a while (years) to implement all the .NET 1.1 functionality and still have a ways to go with 3.5. You'd have to go through the same process.

Obviously your app isn't going to use ALL of .NET though, so the actual effort is going to largely depend on what aspects of .NET you are leveraging.

Thursday, April 17, 2008
 
 
> Obviously your app isn't going to use ALL of .NET though,
> so the actual effort is going to largely depend on what
> aspects of .NET you are leveraging.

Of course I won't be trying to recreate C# in C++.

I guess what I am asking is, what kind of constructs are there in C# that belong uniquely to C# and hence wouldn't be possible to translate to C++?

For example, there are language elements in Python, like lambda, that simply don't exist in C++ and couldn't be replicated.

So, please let me know what those special C#-only elements are w.r.t. C++.
newbie_C#
Thursday, April 17, 2008
 
 
Here's a short list of features in C# that'd be difficult to replicate in C++
* Garbage Collection
* Type Reflection
* The Security model
* Anonymous methods

So, yeah - replicating the part of the class library that you use (more or less) wouldn't be so bad, but you'd run into trouble if you depended on the runtime features.

If you think you might want to port the application to other platforms later, why use .NET in the first place? What are you hoping to gain?

It might make sense to build a C++ core for non-GUI functionality, then do the user-facing elements in C#. That way if you need to port to another polatform, the core code will be portable, and you'll only have to re-do the user interface (which you'd likely want to do anyway).
Mark Bessey Send private email
Friday, April 18, 2008
 
 
> If you think you might want to port the application to
> other platforms later, why use .NET in the first place?
> What are you hoping to gain?

Here is what I am hoping to get:

- Garbage collection is alluring

- Built-in support for various nice things like regex, strings, xml, etc..

- The syntax is easier on the eyes than C++ syntax

- Easier GUI programming

- No (or less) dll hell

- Better web and browser integration
newbie_C#
Friday, April 18, 2008
 
 
From the above "wish list", coupled with your worry about losing platform-independence, why not look to the Java world? It's not perfect, but it offers a good chunk of what you're looking for without locking you onto a specific platform.
BrotherBeal Send private email
Friday, April 18, 2008
 
 
No Java for me, thanks.
newbie_C#
Friday, April 18, 2008
 
 
When I said "Better web and browser integration", I meant the browser can be easily embedded and the framework makes it easier to deal with web formats, interfaces, etc
newbie_C#
Friday, April 18, 2008
 
 
Your first task in C++ will be to write something that will parse C#.  From there, the transition should be easy.
Lance Hampton Send private email
Friday, April 18, 2008
 
 
This might be a crazy suggestion, but you could write ytour code in "Managed C++" - you'd have C++ syntax, but easy access to the .NET framework.

Then when/if you wanted to port to "regular" C++, you'd just have to pick a garbage collection solution (there are several that you can easily adapt), and re-implement some of the .NET framework classes that you use.

On the other hand, I haven't written anything in Managed C++, so I don't know how closely it tracks regular C++ syntax. For all I know, it might be like VB.NET - similar, but not really the same, as the language it derives from.
Mark Bessey Send private email
Friday, April 18, 2008
 
 
The current version of Managed C++ is called C++/CLI.  It's full of syntax extensions to standard C++ so it's really not much different than using C# -- your C++/CLI code simply won't compile on a different system and must be rewritten.  Unless you plan ahead and write most code in regular C++ and reserve C++/CLI to .NET interfacing methods -- but that would lose you the benefit of garbage collection for all your data.

As for newbie's question, the answer is Java, or maybe Python or Ruby.  Writing in C# and then porting to C++ is a non-starter, that's much more work than just writing plain C++ to begin with.
Chris Nahr
Saturday, April 19, 2008
 
 
Agree with all the posters here. C++ to C# is easier but C# to C++ is like forcing you to duplicate all the work MSFT had done in creating C# and .NET (at least the functionalities you need). But it'll be a good experience. Try the managed C++ path and see. Tell us your experience.
Ezani
Tuesday, April 22, 2008
 
 
Qt (www.trolltech.com).  Gives you everything you need but GC and that is a non-issue if you use their memory management idiom.
Anonymous
Friday, May 02, 2008
 
 
Qt is too pricy for me at the moment and the 30-day trial is not enough since I am not working on this project full time. But otherwise, it looks like a great platform.
newbie_C#
Monday, May 05, 2008
 
 
"Back story: I want to switch to .NET for various reasons, but this will lock me to Windows only"

No. It will "lock" you to the .Net runtime, the CLR.
Daniel_DL Send private email
Tuesday, May 06, 2008
 
 
Talk to your trolltech sales rep...they have *big* discounts for mISVs.
Anon for This
Tuesday, May 06, 2008
 
 
>  Talk to your trolltech sales rep...they have *big*
> discounts for mISVs.

Actually did that last week and no they don't. They apparently discontinued the small business program. FYI.
cynic
Tuesday, May 13, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz