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 volatile and variant keywords in C++

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

Share

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

This article mainly explains "how to use the volatile and variant keywords in C++". 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 the volatile and variant keywords in C++.

One or two variables that look a little alike

In fact, many people can only use the volatile keyword and know where to use it, but they don't know the principle of the application. In some multithreaded communications, it is often the place where this keyword is applied, and many people think so. But in fact, this idea is not accurate. The volatile keyword is originally intended for hardware IO operations to prevent access to the cache in IO operations from affecting real data. But the spillover effect of this keyword is that multithreading can also apply this principle (note that it is dangerous in multicore and multi-CPU programming, but in other languages such as Java, it has to be determined by specific language specifications), then the application in hardware IO has become a relatively rare application scenario.

There is also a similar-looking consortium variant in caterpillar 17, and if you use the var variable in other languages, you'll probably get a sense of what it is. Now that it's a union, you can see the usefulness of this keyword, that is, it can express explicit type definitions, while the previously mentioned std::any is not. Take a look at its standard definition in caterpillar 17:

Template class variant

The reason for putting the two together is that sometimes a rookie may confuse the two.

Second, the functions of the two

The class template std::variant represents a type-safe union, which means that you can have any of these types or no values, it cannot hold references, arrays, or void, and if it represents a null value, use std::variantstd::monostate instead. Similarly, when constructing by default, the first type of the union is retained by default. Face std::volatile is more likely to be handled by a compiler's optimization option, which prevents variables from being cached by the compiler. As mentioned earlier, this is often used in multithreading, but its safer application is actually in hardware IO operations.

From this point of view, the difference between the two is quite obvious, although it looks a bit alike at first glance, but it is really not the same thing. Just write the code once and you'll probably remember it clearly. Or simply remember that volatile is a keyword, while variant is a composite type, a union, and a class template.

III. Application examples

Take a look at a variant routine:

# include # include # include int main () {std::variant v, w; v = 12; / v with int int i = std::get (v); w = std::get (v); w = std::get (v); / / same effect as the previous line w = v; / / same effect as the previous line / / std::get (v) / / error: no double// std::get (v) in [int, float]; / / error: legal subscript values are 0 and 1 try {std::get (w); / w contains int instead of float:} catch (const std::bad_variant_access&) {} using namespace std::literals; std::variant x ("abc") will be thrown / / conversion constructor works when there is no ambiguity x = "def"; / / conversion assignment also works when there is no ambiguity std::variant y ("abc"); / / converts to void const * assert (std::holds_alternative (y)) when passing char const *; / / success y = "xyz" s; assert (std::holds_alternative (y)); / / success}

The keyword volatitle gives a judgment routine, but not a multithreaded routine. Generally speaking, this thing should be used carefully. In the case of multi-core and unknown memory order, it is better not to use it. X86 is relatively acceptable for some historical reasons:

# include # include int main () {std::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