concept输入模板参数,输出一个bool类型字面量。
例如:
auto is_int = std::integral<int>;std::cout << "The type of std::integral<int> : " << type_to_string<decltype(std::integral<int>)>() << std::endl;std::cout << "The value type of std::integral<int> : " << type_to_string<decltype((std::integral<int>))>() << std::endl;
输出:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
concept最后输出的结果的值类型是个纯右值,也就是个字面量。我们可以将concept的结果当成字面量使用,也可以用字面量替代concept的结果,在requires中使用。
concept结果当字面量用例如,输入输出:
auto is_int = std::integral<int>;std::cout << std::boolalpha;std::cout << "is_int : " << is_int << std::endl;
输出:
is_int : true
字面量当concept用template<typename T>requires truevoid g(T a) { std::cout << a << std::endl;}int main() { g(10);}
输出:
10
requiresrequires分两种,一种是在template下面的,用来判断concept是否满足的,例如下面的例子:
template<typename T>requires std::integral<T>void g(T a) { std::cout << a << std::endl;}
一种是带表达式的,和一个函数形式类似,例如:
template<typename T>concept can_exec = requires(T a) { a%10;};int main() { std::cout << std::boolalpha; std::cout << can_exec<int> << std::endl; std::cout << can_exec<float> << std::endl;}
输出:
truefalse
其中:
requires(T a) { a%10;};
并不实际求值,只是判断{}内的表达式能否运行。再看个例子:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
0
输出:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
1
所以,对于requires(...){}来说,只要{}中的表达式能运行,就会返回真。
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
2
输出:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
3
requires(...){}主要用来判断表达式能否运行,返回一个bool字面量
当然,也可以直接在template中运用
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
4
输出:
10
requires表达式中还可以再嵌套第一种requires
如果直接使用,会报错:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
6
输出:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
3
还可以这样用
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
8
这样用:
The type of std::integral<int> : boolThe value type of std::integral<int> : bool
9
用来约束lambda表达式
auto is_int = std::integral<int>;std::cout << std::boolalpha;std::cout << "is_int : " << is_int << std::endl;
0
免责声明:本文来自李为民,不代表浮光掠影知识网 - 专注有价值知识的生活内容平台的观点和立场,如有侵权请联系本平台处理。
还没有评论,来说两句吧...