TAILIEUCHUNG - HandBooks Professional Java-C-Scrip-SQL part 69

Tham khảo tài liệu 'handbooks professional java-c-scrip-sql part 69', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Usage To start using variants in your programs include the header boost . This header includes the entire library so you don t need to know which individual features to use later you may want to reduce the dependencies by only including the relevant files for the problem at hand. When declaring a variant type we must define the set of types that it will be capable of storing. The most common way to accomplish this is using template arguments. A variant that is capable of holding a value of type int std string or double is declared like this. boost variant int std string double my_first_variant When the variable my_first_variant is created it ends up containing a default-constructed int because int is first among the types that the variant can contain. We can also pass a value that is convertible to one of those types to initialize the variant. boost variant int std string double my_first_variant Hello world At any give time we can assign a new value and as long as the new value is unambiguously and implicitly convertible to one of the types that the variant can contain it works perfectly. my_first_variant 24 my_first_variant my_first_variant F abulous my_first_variant 0 After the first assignment the contained value is of type int after the second it s a double after the third it s a std string and then finally it s back to an int. If we want to see that this is the case we can retrieve the value using the function boost get like so assert boost get int my_first_variant 0 Note that if the call to get fails which would happen if my_first_variant didn t contain a value of type int an exception of type boost bad_get is thrown. To avoid getting an exception upon failure we can pass a pointer to a variant to get in which case get returns a pointer to the value or if the requested type doesn t match the type of the value in the variant it returns the null pointer. Here s how it is used int val boost get int my_first_variant assert val val 0 The function

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.