首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
gV0拜客生活常识网

#include 
#include 
class MyClass
{
private:
    int x;
    double d;
public:
    MyClass(int xx, double dd)
        : x{ xx }, d{ dd }    {}
    void printdata() { std::cout << "x: " << x << ", d: " << d; }
};
int main()
{
    auto p = std::make_unique(123, 456.789);
    p->printdata();
}

3 C++17

The C++17 standard introduces new language and library features and changes some of the language rules.gV0拜客生活常识网

17.1 Nested Namespaces

Remember how we said we could have nested namespaces ? We can put a namespace into another namespace. We used the following the nest namespaces:gV0拜客生活常识网

namespace MyNameSpace1
{
    namespace MyNameSpace2
    {
        namespace MyNameSpace3
        {
            // some code
        }
    }
}

The C++17 standard allows us to nest namespaces using the namespace resolution operator. The above example can now be rewritten as:gV0拜客生活常识网

namespace MyNameSpace1::MyNameSpace2::MyNameSpace3
{
    // some code
}

17.2 Constexpr Lambdas

Lambdas can now be a constant expression, meaning they can be evaluated during compile-time:gV0拜客生活常识网

int main()
{
    constexpr auto mylambda = [](int x, int y) { return x + y; };
    static_assert(mylambda(10, 20) == 30, "The lambda condition is not true.");
}

An equivalent example where we put the constexpr specifier in the lambda itself, would be:gV0拜客生活常识网

int main()
{
    auto mylambda = [](int x, int y) constexpr { return x + y; };
    static_assert(mylambda(10, 20) == 30, "The lambda condition is not true.");
}

This was not the case in earlier C++ standards.gV0拜客生活常识网

17.3 Structured Bindings

Structured binding binds the variable names to elements of compile-time known expressions, such as arrays or maps. If we want to have multiple variables taking values of expression elements, we use the structured bindings. The syntax is:gV0拜客生活常识网

auto [myvar1, myvar2, myvar3] = some_expression;

A simple example where we bound three variables to be aliases for three array elements would be:gV0拜客生活常识网

int main()
{
    int arr[] = { 1, 2, 3 };
    auto [myvar1, myvar2, myvar3] = arr;
}

Now we have defined three integer variables. These variables have array elements values of 1, 2, 3, respectively. These variables are copies of array elements. Making changes to variables does not affect the array elements themselves:gV0拜客生活常识网

#include 
int main()
{
    int arr[] = { 1, 2, 3 };
    auto [myvar1, myvar2, myvar3] = arr;
    myvar1 = 10;
    myvar2 = 20;
    myvar3 = 30;
    for (auto el : arr)
    {
        std::cout << el << ' ';
    }
}

We can make structured bindings of reference type by using the auto& syntax. This means the variables are now references to array elements and making changes to variables also changes the array elements:gV0拜客生活常识网

#include 
int main()
{
    int arr[] = { 1, 2, 3 };
    auto& [myvar1, myvar2, myvar3] = arr;
    myvar1 = 10;
    myvar2 = 20;
    myvar3 = 30;
    for (auto el : arr)
    {
        std::cout << el << ' ';
    }
}

It is an excellent way of introducing and binding multiple variables to some container-like expression elements.gV0拜客生活常识网

17.4 std::filesystem

The std::filesystem library allows us to work with files, paths, and folders on our system. The library is declared through a header. Paths can represent paths to files and paths to folders. To check if a given folder exists, we use:

相关阅读:

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