Why does C++ need language modifications to be “managed”?

前端 未结 11 1980
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 20:18

Why can\'t a compiler be written that manages what needs to be managed in C++ code (i.e. to make it \"CLR compatible\")?

Maybe with some compromise, li

相关标签:
11条回答
  • 2021-02-14 20:24

    Existing correct code, i.e. code written according to the C++ standard, must not change its behaviour inadvertently.

    0 讨论(0)
  • 2021-02-14 20:34

    Why can't you compile native C++ code targetting the CLR?

    Yes, you guessed it right, there would be too many compromises, that would make it useless. I'd like to name just three examples...

    1.) Templates: C++ supports them, the CLR doesn't (generics are different). So you couldn't use the STL, boost etc. in your code.

    2.) Multiple inheritance: supported in C++, not in CLI. You couldn't even use the standard iostream class and derivatives (like stringstream, fstream), which inherit both from istream and ostream.

    Almost none of the code out there would compile, you couldn't even implement the standard library.

    3.) Garbage collection: Most C++ apps manage their memory manually (using smart pointers etc.), the CLR has automatic memory management. Thus the C++ style "new" and "delete" would be incompatible with "gcnew", making existing C++ code useless for this new compiler.

    If you'd have to root out all the important features, even the standard library, and no existing code would compile... then what's the point?

    0 讨论(0)
  • 2021-02-14 20:34

    first thing to consider is every thing that makes c++ "fast" will disappear. a full garbage collection system in c++ is next to impossible. because c++ you can have pointer nearly anywhere in the code. runtime type information becomes costly if not directly built into the langauge system it self. you can take advantage of true native performance. template will dissappear. true pointers will dissapear. direct access to memory is gone.

    list of things that would have to be enforced

    1. no direct pointers(pointers will get replace with complex refernces)
    2. templates (generics pay for preformance)
    3. simple c-style arrays (will get wrapped with array structures)
    4. programmer no longer has control of whether data is on the stack or
    the heap.
    5. garbage collection will be enforced(this will cause the most changes to the syntax)
    6. runtime type data will get added extensively to the code.
    (larger code size)
    7.  inlining will become more difficult for the compiler
    (no more inline key word)
    8. no more inline assembly.
    9. the new langauge by now will become incompatible c code.(unless you go through hoops) 
    
    0 讨论(0)
  • 2021-02-14 20:35

    Yes, I suppose C++ could become managed. But then .NET would need to be rewritten for C++ and not with a bias towards BASIC. With having many languages all under the same roof. Certain features have got to go. It was a choice between VB.NET or C++.NET, and VB.NET was chosen. Funny thing I hear is that C# is more popular than VB.NET (although I use neither!).

    0 讨论(0)
  • 2021-02-14 20:35

    I agree with 5hammer ! If I left Java and other managed languages that's not for nothing : that's to have FULL control over the computer, access memory manage memory myself, have control over how the computer will run my code, integrate with C libraries (such as Lua). If I loose that flexibility, then I would just leave C++ and fall back to C, and if C becomes managed too, then I would go to assembler.

    Managed languages are the worst ones ever for all gaming platforms/complex programs as they are bounding you in some kine of sandbox with no direct access to the hardware, and are much slower than compiled languages.

    The main purpose of C++ had always been Performence. It's one of the best language for big Games. And without this language performences a lot of games would not exist !

    0 讨论(0)
  • 2021-02-14 20:38

    Well C++/CLI is mostly meant to be a glue between managed and unmanaged code. As such you need to have the ability to mix mangaged an unmanaged concepts. You need to be able to allocate managed and unmanged objects in the same code, so there is no way around separate key words.

    0 讨论(0)
提交回复
热议问题