Examples of ISO C++ code that is not valid C++/CLI

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

I've seen contradictory answers on the internet with regard to whether C++/CLI is a superset of C++ or not.

The accepted answer on this question claims that "technically no", but doesn't provide an examples of non-C++/CLI code that conforms to ISO C++. Another answer on that question cites a book that says the opposite.

So, can you please provide accurate answers with example code that fails on C++/CLI or cite a trusted source (MSDN for example) on this matter?

I had someone this topic come up today and thought I would like to inform myself, but I didn't find any clear answer elsewhere!

回答1:

Valid C++, invalid C++/CLI:

int main() {     int gcnew = 42; } 

gcnew, generic, and nullptr are all reserved words in C++/CLI; nullptr isn't really an issue in C++0x, of course. Herb Sutter blogged about C++/CLI keywords: Under the hood in 2003.

For what it's worth, when I was working on a large mixed codebase (both C++ and C++/CLI), I never ran into syntax issues or trivial issues like this. The giant issue with C++/CLI (in my opinion, of course) is that native code and managed code have completely different resource management paradigms, and it's really easy when writing mixed code to forget which paradigm you need for certain things. I fixed a lot of errors that were due either to an assumption that managed resources were destroyed deterministically or that native resources would be cleaned up by the garbage collector.



回答2:

I am wondering about the plethora of the super-set/extension polemics. C++/CLI is a new language designed to act as a binding between ISO C++ and the ISO CLI. I heartily recommend to read the C++/CLI Design Rationale which clearly states the design goals at the very beginning of the document:

C++/CLI’s mission is to provide direct access for C++ programmers to use existing CLI libraries and create new ones, with little or no performance overhead, with the minimum amount of extra notation, and with full ISO C++ compatibility.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!