+ 33

What language features do professional C++ developers miss when working with C#?

"I know of C# quite well but they are things that are missing (and quite the important ones) that are in C++" - by Baptiste E. Prunier via a Group Chat I've often wondered about such things that are missing from C# which can be found in C++. A few items I'm aware of, but haven't experienced as limitations are as follows: 1. As powerful as generics are in C#, I've heard that templates in C++ are a bit more capable. I've just not explored the limitations of generics compared to C++ templates in real world software development. 2. In languages that only support single inheritance, composition has taken favor over inheritance. I've wondered if modern C++ projects favor multiple inheritance or composition. 3. While C# does support accessing and manipulating pointers when compiled as unsafe (as in unmanaged), I've often wondered where C# pointers fall short compared to modern C++ development needs. Are there any other items that come to mind and how do these rank as short comings for C++ devs?

24th Dec 2019, 4:35 AM
David Carroll
David Carroll - avatar
22 odpowiedzi
+ 19
1. The difference is "quite" simple, in C++ you can use types AND values in templates, and we can use the type (ex: new T(...)). I do not know enough of generics so I can't tell if it is also possible in C# but we can use an unknown number of type / value as template parameter and do some operations on them 2. Multiple inheritance is not, for me, a good thing because sometimes, two entities inherit from a third one and if a fourth one inherit from the two first entities you will have issue. You will either have two instances of the third class instantiated for your fourth class or have to declare that only one should be created when this case arise in the two first classes. The latter choice can cause conflict depending on how the two first classes handle data from their parent. 3. Same here, I never used safe/unsafe so I can't tell if C# can't do it or not. (Part 1/2)
24th Dec 2019, 7:11 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 14
(Part 2/2) In C++ you can open a fragment of a file and not the entire file because we can ask to open a segment of the memory based on its starting point (the file starting point) and its size. Moreover in C++, you can manage memory so that even if you are handling huge amount of data, you can optimize it better than in C# were its garbage is not guaranteed to free the data (I am not sure but someone told me it is the case even if you ask it do to so)
24th Dec 2019, 7:14 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 10
BTW... I welcome anyone with strong C++ knowledge, as well, to provide input even if you don't have professional experience. If you aren't completely certain about a C++ feature that might be missing from C#, go ahead and list it. Just mention you aren't certain and are seeking clarification. I'm truly interested in assembling a list of C++ features missing from C# that are considered must haves for real world application development, not for academic exercises. Hopefully this makes sense. Would love to hear from my virtual SL mates who often answer C++ questions... ~ swim ~ and HonFu, just to mention two of many. 😉
24th Dec 2019, 7:24 AM
David Carroll
David Carroll - avatar
+ 8
Baptiste E. Prunier Nice list to start with. Thanks for posting. 🙏 1. Since generics work only with types, you are correct about not being able to declare generics for values. C# does supports the ability to instantiate from a generic type. T foo = new T(); But this requires a type constraint declared for new() on the generic type, which is reasonable. 😉 I just found an article with a lot more differences between C# generics and C++ templates. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics 2. We share the same view on multiple inheritance. 👌 3. Here's a great references that clears up pointer support and limitations in C#. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/pointer-related-operators C# supports fixed variables that are unaffected by the garbage collector. However, I've not had a scenario yet where the garbage collector didn't cleanup quick enough to merit using this.
24th Dec 2019, 8:03 AM
David Carroll
David Carroll - avatar
+ 7
Also, it is something I do not know how to use well yet but you can manipulate right values (literal values and not variables) inside of variables for optimization purpose
24th Dec 2019, 7:18 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 7
Baptiste E. Prunier Regarding the ability to open a fragment from file without loading the other parts of the file, if I understand this correctly, C# supports this capability using Filestream.Seek() method on an open file stream. https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream.seek There are other options including binary character readers and even implementing a custom class for the IStream interface. Let me know if this is different from what you were referring to.
24th Dec 2019, 8:36 AM
David Carroll
David Carroll - avatar
+ 6
David Carroll for the first point, new was an example but you can use methods of template types or overridden operators on them in C++ That remind me that you can't override the assignment operator in C# while you can in C++ Thanks for all those links, I'll be check them :D
24th Dec 2019, 8:24 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 6
Baptiste E. Prunier I can try to search later when I'm not passing out at 4am EST 😉. But... I'm 100% confident that FileStream will not load any data into memory unless a method like Read() or ReadByte() and so on is invoked. Data can be read character by character, a line at a time, by byte size, etc. The Seek() method will only move to the specified position without loading the data.
24th Dec 2019, 9:17 AM
David Carroll
David Carroll - avatar
+ 5
David Carroll can you clear fixed variables yourself ? The example I had in mind is when you need to load data that is bigger than your RAM. You can easily partially load it in C++, clear the part you do not need anymore to then open a new part of the data
24th Dec 2019, 8:33 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 5
About difference between c++ and c#, I must remember than c++ templates are quite more powerful than c# generics (moreover than template values, in c++ exists the ability to compute values and results at compile-time and there is the ability to check characteristics of a type, changing the behaviour of a class or function in base of those). About memory management, explicit deallocation can be painful, but the power of the lenguage allows to create a custom garbage collector, and many other things...
24th Dec 2019, 1:06 PM
CPPCodeGuru
+ 5
Indirection
26th Dec 2019, 11:39 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 4
CPPCodeGuru Nice points... Thanks for your answer. 🙏 Shtisim Lukshen LOL... Indeed... You've figured me out. 😜 blackwinter and ihab chehab Thanks for your responses. ---- [Please Read for Future Answers] ---- For additional context, I've been working with C#, professionally, since it's alpha release in 2000. There's very little I'm not familiar with in C# by now. 😉 I've become moderately familiar with C++ over the past 23+ years - more out of interest since I've had minimal professional work involving C++ since the late 90s. My question isn't about discovering which language is better or trying to figure out the differences. Rather, my question has more to do with language features often used in modern professional C++ projects that would be sorely missed if working in C#. In my own experience, I've personally not felt as if I've missed out. However, it's hard to miss something you weren't aware was missing. 😜
25th Dec 2019, 6:40 AM
David Carroll
David Carroll - avatar
+ 3
David Carroll the link does not state that the file is not entirely loaded, how does it behave ? Is it default behavior of File class ? I stated this example as a possibility, I don't know C# well enough to know, and I don't know newer versions of C++ to know more differences Ah yes, in C++, lambdas can have varying number of parameters
24th Dec 2019, 9:01 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Baptiste E. Prunier I'm not sure if memory reallocation is possible with fixed statements. However, your question did remind me of the stackalloc operator in C# which gives more flexibility than the fixed statement. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/unsafe-code#dynamic-memory-allocation https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc However, I still suspect that there will be limitations in C# compared to the portable and awesome flexibility in C++. This has been very helpful for me. Again, thank you for the answers.
24th Dec 2019, 9:01 AM
David Carroll
David Carroll - avatar
+ 1
David Carroll I will venture to speculate that you're hired (perhaps by Microsoft?) to work on an insider's project to bring c++ features to C#. At least from this entire conversation, that's the way it seems... 😃
25th Dec 2019, 5:02 AM
Shtisim Lukshen
Shtisim Lukshen - avatar
0
C# does not support multiple inheritance, so you cannot inherit from multiple classes. However, you can use interfaces to implement multiple inheritance. A class can inherit from just one base class, but it can implement multiple interfaces! Therefore, by using interfaces you can include behavior from multiple sources in a class.
24th Dec 2019, 8:18 PM
ihab 🌼
ihab 🌼 - avatar
0
(trying to revise solo c# course) The most common use for generic classes is with collections of items, where operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored. One type of collection is called a stack. Items are "pushed", or added to the collection, and "popped", or removed from the collection. A stack is sometimes called a Last In First Out (LIFO) data structure. For example: class Stack<T> { int index=0; T[] innerArray = new T[100]; public void Push(T item) { innerArray[index++] = item; } public T Pop() { return innerArray[--index]; } public T Get(int k) { return innerArray[k]; } } The generic class stores elements in an array. As you can see, the generic type T is used as the type of the array, the parameter type for the Push method, and the return type for the Pop and Get methods. Now we can create objects of our generic class: Stack<int> intStack = new Stack<int>(); Stack<string> strStack = new Stack<string>(); Stack<Person> PersonStack = new Stack<Person>(); We can also use the generic class with custom types, such as the custom defined Person type.
24th Dec 2019, 8:25 PM
ihab 🌼
ihab 🌼 - avatar
0
C# supports pointers in a limited extent. A C# pointer is nothing but a variable that holds the memory address of another type. But in C# pointer can only be declared to hold the memory address of value types and arrays. Unlike reference types, pointer types are not tracked by the default garbage collection mechanism. For the same reason pointers are not allowed to point to a reference type or even to a structure type which contains a reference type. We can say that pointers can point to only unmanaged types which includes all basic data types, enum types, other pointer types and structs which contain only unmanaged types. https://www.c-sharpcorner.com/article/pointers-in-C-Sharp/ being unmanaged by the garbage collector makes the pointers unsafe in c#
24th Dec 2019, 8:33 PM
ihab 🌼
ihab 🌼 - avatar
0
https://www.c-sharpcorner.com/UploadFile/f0b2ed/understanding-unsafe-code-in-C-Sharp/ In unmanaged code a programmer is responsible for: Calling the memory allocation function Making sure that the casting is done right Making sure that the memory is released when the work is done
24th Dec 2019, 8:33 PM
ihab 🌼
ihab 🌼 - avatar
0
Ку всем, очень крутое приложение
25th Dec 2019, 1:50 AM
Дима Подгорный
Дима Подгорный - avatar