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

前端 未结 11 1371
长发绾君心
长发绾君心 2021-02-14 20:05

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: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?

提交回复
热议问题