首页 > 社交 > 科普中国

C++

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

template 
concept MustBeIncrementable = requires (T x) { x += 1; };

To use this concept in a template, we write:gV0拜客生活常识网

template
void myfunction(T x)
{
    // code goes in here
}

Another way to include the concept into our template is:gV0拜客生活常识网

template requires MustBeIncrementable 
void myfunction(T x)
{
    // code goes in here
}

A full working example would be:gV0拜客生活常识网

#include 
#include 
template 
concept MustBeIncrementable = requires (T x) { x ++; };
template
void myfunction(T x)
{
    x += 1;
    std::cout << x << '
';
}
int main()
{
    myfunction(42); // OK
    myfunction(123); // OK
    myfunction(345.678); // OK
}

This concept ensures our argument x of type T must be able to accept operator ++, and the argument must be able to be incremented by one. This check is performed during the compile-time. The requirement is indeed true for types char, int, and double. If we used a type for which the requirement is not fulfilled, the compiler would issue a compile-time error.gV0拜客生活常识网

We can combine multiple concepts. Let us, for example, have a concept that requires the T argument to be an even or an odd number.gV0拜客生活常识网

template 
concept MustBeEvenOrOdd = requires (T x) { x % 2; };

Now our template can include both the MustBeIncrementable and MustBeEvenOrOdd concepts:gV0拜客生活常识网

template requires MustBeIncrementable && MustBeEvenNumber;
void myfunction(T x)
{
    // code goes in here
}

The keyword requires is used both for the expression in the concept and when including the concept into our template class/function.gV0拜客生活常识网

The complete program, which includes both concept requirements, would be:gV0拜客生活常识网

#include 
#include 
template 
concept MustBeIncrementable = requires (T x) { x++; };
template 
concept MustBeEvenOrOdd = requires (T x) { x % 2; };
template requires MustBeIncrementable && MustBeEvenOrOdd
void myfunction(T x)
{
    std::cout << "The value conforms to both conditions: " << x << '
';
}
int main()
{
    myfunction(123); // OK
    myfunction(124); // OK
    myfunction(345); // Error, a floating point number is not even // nor odd
}

In this example, the template will be instantiated if both concept requirements are evaluated to true during compile time. Only the myfunction(123); and myfunction(124); functions can be instantiated and pass the compilation. The arguments of types char and int are indeed incrementable and can be either even or odd. However, the statement myfunction(345); does not pass a compilation. The reason is that the second requirement MustBeEvenOrOdd is not fulfilled as floating-point numbers are neither odd nor even.gV0拜客生活常识网

Important! Both concepts say: for every x of type T, the statement inside the code-block { } compiles and nothing more. It just compiles. If it compiles, the requirement for that type is fulfilled.gV0拜客生活常识网

If we want our type T to have a member function, for example, .empty() and we want the result of that function to be convertible to type bool, we write:gV0拜客生活常识网

template 
concept HasMemberFunction requires (T x)
{
    { x.empty() } -> std::convertible_to(bool);
};

There are multiple predefined concepts in the C++20 standard. They check if the type fulfills certain requirements. These predefined concepts are located inside the header. Some of them are:

相关阅读:

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