首页 > 社交 > 科普中国

C++

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

// mylibrary.cpp
#include "mylibrary.h"
int myfunction()
{
    return 123;
}

In our main.cpp file we also include the above header file and call the function:gV0拜客生活常识网

// main.cpp
#include "mylibrary.h"
int main()
{
    int x = myfunction();
}

We include the same header multiple times. This increases compilation time. Modules are included only once, and we do not have to separate the code into interface and implementation. One way is to have a single module file, for example, mymodule.cpp where we provide the entire implementation and export of this function.gV0拜客生活常识网

To create a simple module file which implements and exports the above function, we write:gV0拜客生活常识网

// mymodule.cpp
export module mymodule;
export int myfunction() { return 123; }

Explanation: the export module mymodule; line says there is a module called mymodule in this file. In the second line, the export specifier on the function means the function will be visible once the module is imported into the main program.gV0拜客生活常识网

We include the module in our main program by writing the import mymodule; statement.gV0拜客生活常识网

// main.cpp
import mymodule;
int main()
{
    int x = myfunction();
}

In our main program, we import the module and call the exported myfunction() function.gV0拜客生活常识网

A module can also provide an implementation but does need to export it. If we do not want our function to be visible to the main program, we will omit the export specifier in the module. This makes the implementation private to the module:gV0拜客生活常识网

export module mymodule;
export int myfunction() { return 123; }
int myprivatefunction() { return 456; }

If we have a module with a namespace in it, and a declaration inside that namespace is exported, the entire namespace is exported. Within that namespace, only the exported functions are visible Example:gV0拜客生活常识网

// mymodule2.cpp
export module mymodule2;
namespace MyModule
{
    export int myfunction() { return 123; }
}

main2.cpp:gV0拜客生活常识网

import mymodule2;
int main()
{
    int x = MyModule::myfunction();
}

20.2 Concepts

类型总是定义了一组特定的操作。模板实现的泛型有些不能满足特定类型的需要,一种方法是类型特化,concepts的方法是显式声明类型的特定要求。gV0拜客生活常识网

Remember the class templates and function templates providing generic types T? If we want our template argument T to satisfy certain requirements, then we use concepts. In other words, we want our T to satisfy certain compile-time criteria. The signature for a concept is:gV0拜客生活常识网

template
concept concept_name = requires (T var_name) { reqirement_expression; };

The second line defines a concept name followed by a reserved word requires, followed by an optional template argument T and a local var_name, followed by a requirement_expression which is a constexpr of type bool.gV0拜客生活常识网

In a nutshell, the concept predicate specifies the requirements a template argument must satisfy in order to be used in a template. Some of the requirements we can write ourselves, some are already pre-made.gV0拜客生活常识网

We can say that concepts constrain types to certain requirements. They can also be seen as a sort of compile-time assertions for our template types.gV0拜客生活常识网

For example, if we want a template argument to be incrementable by one, we will specify the concept for it:

相关阅读:

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