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

What are the 15 obscure features of C++ language?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you what are the 15 obscure features of C++ language, which are concise and easy to understand. I hope you can get something through the detailed introduction of this article.

This list collects some of the Obscure features of the C++ language, which I have collected over the years through my study of various aspects of the language. C++ is very huge. I can always learn some new knowledge. Even if you already know C++ like the back of your hand, I hope you can learn something from the list. The features listed below are sorted from shallow to deep according to the degree of obscurity.

1. The true meaning of square brackets

two。 The most annoying analysis

3. Alternative operation marker

4. Redefine keyword

5. Placement new

6. Branch while declaring variables

7. Reference modifiers for member functions

8. Turn to complete template metaprogramming

9. Pointer operator to a member

10. Static instance method

11. Overload + + and-

twelve。 Operator overloading and check order

13. Function as a template parameter

14. The parameters of the template are also templates.

15.try block as a function

The true meaning of square brackets

Ptr [3] used to access array elements is actually an abbreviation of * (ptr + 3), which is equivalent to using * (3 + ptr), so the reverse is also equivalent to 3 [ptr]. Using 3 [ptr] is completely effective code.

The most annoying analysis

The word "most vexing parse" was coined by Scott Meyers because the ambiguity of C++ grammatical statements can lead to counterintuitive behavior:

/ / is this explanation correct? / / 1) will variables of type std::string be instantiated through std::string ()? / / 2) A function declaration that returns a std:: string value with a function pointer argument, / / the function also returns a std::string but no arguments? Std::string foo (std::string ()); / / or is this correct? / / 1) will type int variables be instantiated through int (x)? / / 2) A function declares that it returns an int value and has an argument. / / is the parameter an int variable named x? Int bar (int (x))

In both cases, the C++ standard requires a second interpretation, even if it looks more intuitive. Programmers can disambiguate by enclosing the initial values of variables in parentheses:

/ / use parentheses to disambiguate std::string foo ((std::string (); int bar ((int (x)

The second reason for ambiguity is that int y = 3; equivalent to int (y) = 3

Translator's note: I'm a little confused at this point. Here are my test cases under gaming +:

# include # include using namespace std; int bar (int (x)); / / equivalent to int bar (int x) string foo (string ()); / / equivalent to string foo (string (*) () string test () {return "test";} int main () {cout

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

Development

Wechat

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

12
Report