What can C++ do that C# can’t?

Asked

Viewed 14,277 times

34

Being a programmer . Net but with an old passion for C++ (which has grown since C++11), I was with this curiosity.

I know . Net can be "extended" with C++/CLI, but I would like to know what C# pure cannot do, while C++ can.

Note: I don’t want opinions here, and I also don’t want to start a flame war. I want facts. I just want to know what the title of the question says, I don’t care if you prefer one language or another because is easier do X on it.

Updating: After 4 responses and a few hours, I’m judging the @utluiz as the one that best answered my question, so I am marking as accepted. The @Flavius is also good, but I found the one from @utluiz more correct.

The two other answers mainly talk about the framework and performance, which are obvious to those who are not new to the subject. I’m not saying they were bad answers, were just not good answers to that one question. (Apart from the fact that the framework is quite stable and performance is not a critical subject.)

  • 2

    Maybe the main thing is to run in non-MM-Windows environments.

  • 2

    @user2478690 Have you heard of Project Mono?

  • 1

    It’s not the direct answer to your question but it’s an interesting addition: "C# is slower than C++ because it runs on a virtual machine and not on the native machine".

  • 3

    open question (non-specific), very susceptible to opinions, subject to different types of answers. Vote to close...

  • @pepper_chico Negative. If you look at my grade and update, you’ll see that if there’s something I’m not accepting here, it’s opinions. Before voting to close, see how is the acceptance of these questions in Sopt.

  • 1

    As I’ve already said, you’re asking a kind of question that’s not well defined/described, so the answers that come up are shots, based on what each one understands of the question that’s confused, and I’m not talking about wanting to give any opinion, the fact is, there is no escape from it, for the very understanding of the question is the reader’s choice, and the very understanding of answered is on his part, and not a thing that is said to be impartial.

  • @alacerda C# is not executed by a virtual machine. The code is compiled in native code at the time of execution by a JIT compiler.

  • 2

    C++ can give you a salary of more than four decimal places.

  • 1

    @Renan The joke was good, but I still believe that what makes the salary depends more on the programmer than on the xD language (and, unfortunately, depends more on the market than on the programmer -- although I love C++, there is almost no more market for it)

  • @Bigown I had not tried to me much his answer. But she is perfect.

  • I decided to answer because I didn’t think any answer went anywhere near what you wanted. Perfect is your exaggeration, but I tried to find everything that’s important different :)

Show 6 more comments

7 answers

30


Summary:

  • Viable operating systems, drivers for existing operating systems or any operation requiring direct access to hardware.
  • Manual memory management and optimization including enabling applications real team.
  • Metaprogramming with templates and preprocessing.
  • Multiple inheritance.
  • Hands-free handling.
  • A series of small syntactic and semantic functionalities.

Some have already said that in theory you can do everything. Of course this is true and of course there are differences of how to do it and the result obtained. Some things are easier to do in one language than I am in another. In a way, the request not to talk about what is easier eliminates anything that can talk about the language itself. As I think you have objective ways of talking about the facilities I will try to answer.

Ambience

  • Allows use on virtually any platform while C# essentially only works on all platforms mainstream, which are the most important, the 3 main desktops, the 3 main mobiles and various servers.
  • Wheel without dependence on a Runtime great. Applications can be very lean. C# has improved a bit this.
  • You don’t have to Jittar the code before running. Now there is the .NET Native.
  • Direct access to hardware and operating system allowing the creation of drivers, for example.
  • Easier integration with C.

Memory management

  • Has absolute control over memory management.
  • Allows total control of the layout and place of storage of data structures. It is possible to save up to the last byte and put in place great for the situation.
  • Deterministic release of resources as part of the object, preventing liberation from being forgotten because it did not put a using.
  • Allows the implementation of some Garbage Collector.

Optimizations

  • Allows you to search for optimizations that save the last processor cycle. And has some more aggressive optimizations in the compiler.
  • Memory saving in several aspects.
  • Applications "totally" real team. Some games with main loop very heavy no slack for garbage collection are only viable in C++.
  • Using undefined behaviors to obtain optimizations and capabilities according to the platform.
  • Use the basic numerical data with the layout most suitable for the platform.
  • Inclusion of Assembly code.
  • Have fully build-time generic implementations.
  • Saves the overhead of vtable when she’s not needed.
  • There are more choices to optimize code while maintaining proper semantics (move Semantics, initiators and constexpr for example).

Syntax

  • Create pseudo-syntax and use other preprocessor "facilities".
  • Group member declarations by visibility without directive #region.

Typing

  • Has union although C# has a limited form.
  • It has the security that references (not to be confused with pointers) do not contain null value. C# will have something like this.
  • Freedom to create object hierarchy however you want, without a predefined basic hierarchy.
  • Use types in addition to bool as a result of conditional deviation.
  • Has several types string by default.
  • Has more control over constancy of values, including consistency.
  • The functions may have the type of feedback inferred.

Specific resources

  • Has independent functions that do not need to be class members.
  • Multiple inheritance of abstract classes, not only through interfaces.
  • Use a sublanguage with templates which is absurdly more powerful than Generics. Only this item can unfold in several others.
  • Allows goto a little freer.
  • Lets you create literals for types.
  • There are more operators that can be overloaded.
  • There is more control in the form of capturing variables in closures.
  • The throw lets launch any class.
  • The pointers are much more flexible and powerful, allows arbitrary access to memory.

Patchwork

  • Choosing a wider range of memory models to address parallelism and competition.
  • It can blow up your computer and screw your brain (it’s just a joke, okay?)

Some difficulties of C#, especially the first ones listed can be solved soon with the .NET Native. This is why it is good to bear in mind that the difficulty often lies in the implementation of language and not in the language itself. Some of these limitations have long been minimized with the Mono.

One could say that C++ allows you to create an operating system or at least it would be easier. C# also allows and is even easier. Of course it may have some limitations, but it is possible and in fact it has already been done. You may even need help from another language (Assembly), but C++ also needs.

Some items could generate various details. There are so many things that the list can grow indefinitely.

Having all these capabilities doesn’t mean C++ is better. It’s different. Because some of these things that C++ can do is easily interpreted as downside. And C# also has many things more "ready" or more powerful than C++. Just the question doesn’t matter here.

23

C++ allows for:

  • multiple inheritance against interfaces in C#.
  • manual memory management against GC in C#.
  • templates against Generics in C#.
  • inline Assembly.
  • Ability to explicitly displace an object with delete.
  • Actually, I don’t think that answers the question. Does anyone think I should exclude?

  • Multiple inheritance is always controversial, rs. I think valid as part of the answer, but not comprehensive. Even so, +1

  • Do not exclude. C++ can, C# can’t. That’s not it?

  • 1

    @Andréleria Rereading the question I thought you wanted something in terms of end result and not language resources. But if it’s good for you, great!

  • The end result depends more on the programmer than on the language. For example, if it were C++ vs Java, it would be worth Lambda Expressions (and, similarly, Multiple Inheritance).

  • 1

    Manual memory management is possible in C# as well. http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

  • 1

    @Renan Although You can delete an object, this is not the same as dislocating the memory, it just occurs that the object is eligible for collection. So much so that in the link you posted, the topic "Explicit Release of Resources" says that to release open resources immediately it is recommended to use a method Dispose.

  • 2

    @utluiz It was bad. Noted and learned.

  • 1

    When I said deletar, actually was talking about the "destructor" being activated, for example by removing the references of variables for the object. Who owns the delete is C++ and not C#.

  • 6

    Remembering that "modern" C++ code avoids using new and delete; it uses stack objects and "smart pointers", and the RAII Pattern blessed by Stroustrup :)

Show 5 more comments

12

As a language, C# is not owed anything, nor in performance.

The problem with C# is that it implies a framework that is controlled by Microsoft, although there is the free implementation, Miguel de Icaza’s Mono/Xamarin. I came to use it in "serious" application that had to run on Mac/Linux/Windows and the allocated developer preferred to use C#.

If you develop code whose "durability" should be very long, you have to think twice about which pole will tie the donkey. Tomorrow the framework changes, the day after tomorrow it can be abandoned... See what happened to the Uses software that bet their chips on Delphi. Change the version of Delphi, break everything, there are many people out there stuck to paleolithic versions of Delphi because the program and/or third party components are incompatible with new versions.

Now, if you’re going to do something that you don’t have to maintain for decades, and it’s more Microsoft-centric, go ahead and use C#. If you’re making apps for Windows Phone, where by definition you umbilically depend on the framework, there’s nothing to discuss.

  • 1

    +1 for citing Mono, including Xamarim (based on Mono) is a good solution to the problem of developing multi-platform for mobile.

12

C++ is Turing-complete, C# too, so with computational effort it can be done the same with both. Limitations will appear due to the environment under which they will be submitted and not to a language limitation.

8

  1. Direct access to memory address. Even if extended with C++ the VM does not allow you to directly access memory addresses.
  2. Write Hardware Drivers
  • 1

    Access to addresses is not allowed with the unsafe?

  • 1

    @Andréleria yes, it is allowed with unsafe. I believe that with this it would also be possible to write hardware drivers.

  • @Flavio Citation needed in statement number 2.

  • @Renan http://stackoverflow.com/questions/75886/c-sharp-driver-development

4

Nothingness.

Both languages are Turing-complete, as pepper_chico said. That means it can be very easier do some things in one instead of the other, but nothing one does is impossible to do in the other.

In the latter case, it is possible to write a C++ compiler in C# and embed it in the C program#.

2

Depends on the task. If you are critical in performance C/C++ is recommended. For example, I developed an application that runs as a service, collecting process performance counters. It does exactly what was designed, but has a very high cost of resource consumption compared to the version that was remade in C++. There are two high cost factors: memory with large consumption peaks and computational cost with peaks of 25 % consumption. In an environment where computational resources should be directed to the end object, monitoring costs should not exceed 3% to 5% of the total.
The C++ version is in the homologation phase, but has already shown excellent performance with low memory consumption.

  • The question refers to language resources, not performance / memory usage.

  • @Andréleria then describe her question better and don’t leave it so open and susceptible to being closed. The way it is, I vote it closes, why is not explicit in anything and says it does not seek opinion but is the only type of answer that fits the way it is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.