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 std::any of C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces C++ 's std::any how to use the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this C++ std::any how to use the article will have a harvest, let's take a look at it.

Generally speaking, C++ is a language with type binding and type safety. The value object is declared to have a specific type that defines which operations are possible and how they behave. Value objects cannot change their type.

Std: any is a value type that can be changed while still being type-safe. That is, objects can hold values of any type, but they know which type of value is currently saved. When declaring objects of this type, you do not need to specify possible types.

The trick is that the object has both the contained value and the type that contains the value using typeid. Because this value can be of any size, memory can be allocated on the heap to encourage implementations to avoid dynamic allocation of small objects. That is, if a string is allocated, the object allocates memory for the value and copies the string, while also storing the allocated string internally. Later, you can perform a run-time check to determine the type of the current value, and use any_cast to get the value.

1. Use std::any

The following example demonstrates std::any:

Std::any a; / an is emptystd::any b = 4.3; / / b has value 4.3 of type double a = 42; / a has value 42 of type intb = std::string {"hi"}; / / b has value "hi" of type std::string if (a.type () = = typeid (std::string)) {std::string s = std::any_cast (a); useString (s) } else if (a.type () = = typeid (int)) {useInt (std::any_cast (a));}

You can declare that std::any is empty or initialized by a specific type of value. The type of the initial value becomes the type of the contained value. By using the member function type (), you can check the type of value contained against any type of type ID. If the object is empty, the object type ID is typeid (void). To access the included values, you can use the std::any_cast method:

Auto s = std::any_cast (a)

If the conversion fails because the object is empty or contains a mismatch, a std::bad_any_cast is thrown. Therefore, without checking or knowing the type, it is best to implement the following functions:

Try {auto s = std::any_cast (a);...} catch (std::bad_any_cast& e) {std::cerr

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