首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
<< o.c; // accessing o.c or o.x is undefined behavior at this point }

C++17 introduces a new way of working with unions using the std::variant class template from a header. This class template offers a type-safe way of storing and accessing a union. To declare a variant using a std::variant, we would write:gV0拜客生活常识网

#include 
int main()
{
    std::variant myvariant;
}

This example defines a variant that can hold three types. When we initialize or assign a value to a variant, an appropriate type is chosen. For example, if we initialize a variant with a character value, the variant will currently hold a char data member. Accessing other members at this point will throw an exception. Example:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::variant myvariant{ 'a' }; // variant now holds // a char
    std::cout << std::get<0>(myvariant) << '
'; // obtain a data member by // index
    std::cout << std::get(myvariant) << '
'; // obtain a data member // by type
    myvariant = 1024; // variant now holds an int
    std::cout << std::get<1>(myvariant) << '
'; // by index
    std::cout << std::get(myvariant) << '
'; // by type
    myvariant = 123.456; // variant now holds a double
}

We can access a variant value by index using the std::get(variant_name) function. Or we can access the variant value by a type name using: std::get(variant_name). If we tried to access a wrong type or wrong index member, an exception of type const std::bad_variant_access& would be raised. Example:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::variant myvariant{ 123 }; // variant now holds an int
    std::cout << "Current variant: " << std::get(myvariant) << '
';
    try
    {
        std::cout << std::get(myvariant) << '
'; // exception is // raised
    }
    catch (const std::bad_variant_access& ex)
    {
        std::cout << "Exception raised. Description: " << ex.what();
    }
}

We define a variant that can hold either int or double. We initialize the variant with a 123 literal of type int. So now our variant holds an int data member. We can access that member using the index of 0 or a type name which we supply to the std::get function. Then we try to access the wrong data member of type double. An exception is raised. And the particular type of that exception is std::bad_variant_access. In the catch block, we handle the exception by parsing the parameter we named ex. A parameter is of type std::bad_variant_access, which has a .what() member function that provides a short description of the exception.gV0拜客生活常识网

20.4 C++20

The C++ 20 standard promises to bring some big additions to the language. Its impact on the existing standards is said to be as big as the C++11 was to a C++98/C++03 standard. At the time of writing, the C++20 standard is to be ratified around May 2020. The full implementation and the support in the compilers should follow. Some of the following things may, at first glance, seem intimidating, especially when beginning C++. However, do not worry. At the time of writing, none of the compilers fully support the C++20 standard, but that is about to change. Once the compilers fully support the C++20 standard, trying out the examples will be much easier. With that in mind, let us go through some of the most exciting C++20 features.gV0拜客生活常识网

20.1 Modules

Modules are the new C++20 feature, which aims to eliminate the need for the separation of code into header and source files. So far, in traditional C++, we have organized our source code using headers files and source files. We keep our declarations/interfaces in header files. We put our definitions/implementations in source files. For example, we have a header file with a function declaration:gV0拜客生活常识网

mylibrary.hgV0拜客生活常识网

#ifndef MYLIBRARY_H
#define MYLIBRARY_H
int myfunction();
#endif // !MYLIBRARY_H39

Here we declare a function called myfunction(). We surround the code with header guards, which ensures the header file is not included multiple times during the compilation. And we have a source file with the function definition. This source file includes our header file:

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。