Beyond the C ++ standard library: An Introduction to boost-library 6.1 any

Source: Internet
Author: User

Part II: container and Data Structure

This section discusses three libraries: boost. Any, boost. variant, and boost. tuple. In a sense, they are all containers, although they have nothing to do with the container class of the standard library. They are very useful libraries. Many people, like me, use them every day to solve programming problems. They solve problems that are not covered by the C ++ or C ++ standard library, so they are indeed a very important extension of our library toolbox. It is interesting to think that the effectiveness of the basic data structure will affect our programming and design methods. Without such a data structure, we will need to implement it ourselves, and we usually only consider implementation in the scope of solutions, which will greatly limit the reusability of our work results. Of course, there is a common topic for all programming styles, and the compromise between universality and basic nature can well complete this work. Library flexibility not only allows us to complete the current task, but also allows us to solve more similar problems in the future. These libraries also expand our c ++ vocabulary in a sense, and more users use these libraries, so more communities will use the same vocabulary. I'm sure that every library in this chapter should have a place in every C ++ toolbox.

Library 6. Any

 

Any library how to improve your program?
  • Any type of secure storage and secure Retrieval

  • Store different types of methods in the standard library container

  • Transfer Type without knowing the type

Any database provides a type,AnyWhich can be stored in any type and retrieved later without losing the type security. It is a variable type compound: it can hold any type, but you must know the type to retrieve the value. There are many times that you want to store irrelevant types in the same container. There are many times that some codes only want to transmit data from one pointer to another, regardless of the data type. On the surface, these things are easy to do. They can be implemented through a non-class type, suchVoid *. They can also be implemented by using a Union with different types. Many variable types are implemented by some type identification mechanism. Unfortunately, all of these lack type security, and we should intentionally bypass the type system only in the most controllable circumstances. The containers of the standard library need to be unique by the types they contain, which means it is impossible to store different types of elements into the container. Fortunately, the solution is not necessarilyVoid *Because the any database allows you to store different types and retrieve them later. There is no way to retrieve the saved value without knowing the actual type. The type is safe and thus guaranteed.

When designing a framework, it is impossible to know which types should be used with the framework class in advance. A common method is to require framework users to comply with an interface or derive from a base class provided by the framework. This is reasonable because the framework may need to communicate with different advanced classes for use. However, in this case, the Framework does not need to know (or cannot) any relevant information about the type of storage or acceptance. Do not bypass the type system to useVoid *Method, the framework can be usedAny.

 

How does any apply to the standard library?

An important feature of any is that it provides the ability to store different types of objects to the standard library container. It is also a variable data type, which is exactly what the C ++ standard library needs and lacks.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.