Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the cplusplus of C++

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use C++ 's cplusplus". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use C++ 's cplusplus".

[1] _ _ cplusplus

In code written in a mixture of C and C++, you will often see a declaration in the header file in the following form:

# ifdef _ cplusplusextern "C" {# endif// specific code. # ifdef _ cplusplus} # endif

This type of header file can be compiled by # include into a C file or by # include into a C++ file.

Key point: because extern "C" can restrain C++ from renaming symbols (symbol) such as function names, variable names, etc. (name mangling)

Therefore, the variables, function names and other symbols in the compiled C object file and C++ object file are the same (otherwise not the same), and the linker can reliably link the two types of object files.

In this way, this practice has become a typical practice of mixing header files between C and C++.

To make it easier to understand, for example:

# ifdef _ cplusplusextern "C" {# endifvoid* memset (void*, int, size_t); # ifdef _ cplusplus} # endif

Because C and C++ are different after all, and in order to achieve a program that is compatible in C and C++, it would be too troublesome to define two sets of header files.

So with regard to the emergence of the keyword _ _ cplusplus, this keyword is unique in C++. _ _ cplusplus is actually C++.

If the above code appears in the C++ file, after conditional compilation, the code becomes:

/ * result of conditional compilation in C++ file * / extern "C" {void* memset (void*, int, size_t);}

If the above code appears in the C file, after conditional compilation, the code becomes:

/ * result of conditional compilation in / * C file * / void* memset (void*, int, size_t)

[2] key points of Central11 _ _ cplusplus

In view of the above, programmers may think that the macro _ _ cplusplus has only two states: "defined" and "undefined".

In fact, this is not the case, the macro _ _ cplusplus is usually defined as an integer value.

And as the standard changes, the _ _ cplusplus macro will generally be a larger value than in previous standards.

For example, in the Category 03 standard, the value of _ _ cplusplus is scheduled to be 199711L, while in the Clover 11 standard, the macro _ _ cplusplus is predefined as 201103L.

This change can be used by the code, for example, when the programmer wants to determine that the code is compiled with a compiler that supports Category 11, he can detect it as follows:

# if _ _ cplusplus < 201103L#error should use C++ 11 implementation#endif Thank you for reading, the above is the content of "how to use the cplusplus of C++". After the study of this article, I believe you have a deeper understanding of how to use the cplusplus of C++, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report