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 does C++ use T {e} notation to construct objects

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how C++ uses T {e} notation to construct objects". In daily operation, I believe many people have doubts about how C++ uses T {e} notation to construct objects. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how C++ uses T {e} notation to construct objects". Next, please follow the editor to study!

ES.64: constructing objects using T {e} notation

Reason (reason)

The T {e} construction syntax clearly expresses the way you want it to be constructed. T {e} construction syntax does not allow narrowing transformations. T {e} is a common and only safe way to construct a T value from the expression e. Conversion notation T (e) and (T) e are neither safe nor universal.

Example (sample)

For built-in types, this construction prevents narrowing and numerical reinterpretation.

Void use (char ch, int I, double d, char* p, long long lng)

{

Int x1 = int {ch}; / / OK, but redundant

Int x2 = int {d}; / / error: double- > int narrowing; use a cast if you need to

Int x3 = int {p}; / / error: pointer to- > int; use a reinterpret_cast if you really need to

Int x4 = int {lng}; / / error: long long- > int narrowing; use a cast if you need to

Int Y1 = int (ch); / / OK, but redundant

Int Y2 = int (d); / / bad: double- > int narrowing; use a cast if you need to

Int Y3 = int (p); / / bad: pointer to- > int; use a reinterpret_cast if you really need to

Int Y4 = int (lng); / / bad: long long- > int narrowing; use a cast if you need to

Int Z1 = (int) ch; / / OK, but redundant

Int Z2 = (int) d; / / bad: double- > int narrowing; use a cast if you need to

Int Z3 = (int) p; / / bad: pointer to- > int; use a reinterpret_cast if you really need to

Int Z4 = (int) lng; / / bad: long long- > int narrowing; use a cast if you need to

}

When T (e) or (T) e notation is used to convert integers to pointers, the result depends on the implementation, and there is no portability between different integers and pointer lengths (64bit?32bit?).

Note (Note)

Avoid type conversions (explicit conversions). If a conversion is necessary, a named conversion is used.

Note (Note)

When unambiguous, the T can be left out of T {e}.

If the purpose is clear, T can be separated from T {e}.

Note

The construction notation is the most general initializer notation.

Construction notation is the most common initialization notation.

Exception (exception)

Std::vector and other containers were defined before we had {} as a notation for construction. Consider:

Std::vector and other containers existed before {} could be used as a construction token. Consider the following code:

Vector vs {10}; / / ten empty strings

Vector vi1 {1,2,3,4,5,6,7,8,9,10}; / / ten elements 1.. 10

Vector vi2 {10}; / / one element with the value 10

How do we get a vector of 10 default initialized ints?

How can I get a vector that contains 10 elements that have been initialized by default?

Vector v3 (10); / / ten elements with value 0

Initializing the number of elements with () instead of {} is a common practice (dating back to the 1980s), which is hard to change, but it is still a design error: when the number of elements of a feature type may be confused (such as integers, translator's note), it is necessary to disambiguate. It is customary to interpret {10} as a list containing only one element, while (10) represents the number of elements.

There is no need to repeat this error in the new code. We can define a type that represents the number of elements.

Struct Count {int n;}

Template

Class Vector {

Public:

Vector (Count n); / / n default-initialized elements

Vector (initializer_list init); / / init.size () elements

/ /...

}

Vector v1 {10}

Vector v2 {Count {10}}

Vector v3 {Count {10}}; / / yes, there is still a very minor problem

The main remaining problem is finding a suitable name for Count.

Enforcement (implementation recommendations)

Flag the C-style (T) e and functional-style T (e) casts.

Represents all code converted using C style (T) e and function style T (e).

At this point, the study on "how C++ uses T {e} notation to construct objects" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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