网站首页 > 博客文章 正文
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> : bool
The 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 true
void g(T a) {
std::cout << a << std::endl;
}
int main() {
g(10);
}
输出:
10
requires
requires分两种,一种是在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;
}
输出:
true
false
其中:
requires(T a) {
a%10;
};
并不实际求值,只是判断{}内的表达式能否运行。再看个例子:
template<typename T>
concept can_exec1 = requires(T a) {
false;
};
int main() {
std::cout << std::boolalpha;
std::cout << can_exec1<int> << std::endl;
std::cout << can_exec1<float> << std::endl;
}
输出:
true
true
所以,对于requires(...){}来说,只要{}中的表达式能运行,就会返回真。
int main() {
auto can_exec2 = requires(int a, int b){
a+b;
};
std::cout << std::boolalpha;
std::cout << can_exec2 << std::endl;
}
输出:
true
requires(...){}主要用来判断表达式能否运行,返回一个bool字面量
当然,也可以直接在template中运用
template<typename T>
requires
requires{
false;
}
void g1(T a) {
std::cout << a << std::endl;
}
g1(10)
输出:
10
requires表达式中还可以再嵌套第一种requires
如果直接使用,会报错:
// int main() {
// auto can_exec2 = requires std::integral<int>;
// std::cout << std::boolalpha;
// std::cout << can_exec2 << std::endl;
// }
// error: expected '{' before 'std'
int main() {
auto can_exec2 =requires{
requires std::integral<int>;
};
std::cout << std::boolalpha;
std::cout << can_exec2 << std::endl;
}
输出:
true
还可以这样用
int main() {
float a = 2.2;
int b = 3;
auto f = [&]<typename T>(T a) {
auto can_exec2 = requires(T value) { requires std::integral<T>; };
return can_exec2;
};
std::cout << std::boolalpha;
std::cout << f(a) << std::endl;
std::cout << f(b) << std::endl;
}
这样用:
template <typename T>
requires requires { requires std::integral<int>; }
void g1(T a) { std::cout << a << std::endl; }
用来约束lambda表达式
auto f = [&]<typename T>requires std::integral<T>(T a) {
};
猜你喜欢
- 2024-10-12 C++核心准则T.24:用标签类或特征区分只有语义不同的概念
- 2024-10-12 用苹果发布会方式打开C++20(苹果在哪开发布会)
- 2024-10-12 C++核心准则T.25:避免互补性约束(规矩是一种约束,一种准则)
- 2024-10-12 C++核心准则T.21:为概念定义一套完整的操作
- 2024-10-12 C++核心准则T.5:结合使用泛型和面向对象技术应该增强效果
- 2024-10-12 C++经典书籍(c++相关书籍)
- 2024-10-12 C++一行代码实现任意系统函数Hook
- 2024-10-12 C++核心准则T.11:只要可能就使用标准概念
- 2024-10-12 C++核心准则T.48:如果不能用概念,用enable_if
- 2024-10-12 C++核心准则T.13:简单、单类型参数概念使用缩略记法更好
你 发表评论:
欢迎- 最近发表
-
- 给3D Slicer添加Python第三方插件库
- Python自动化——pytest常用插件详解
- Pycharm下安装MicroPython Tools插件(ESP32开发板)
- IntelliJ IDEA 2025.1.3 发布(idea 2020)
- IDEA+Continue插件+DeepSeek:开发者效率飙升的「三体组合」!
- Cursor:提升Python开发效率的必备IDE及插件安装指南
- 日本旅行时想借厕所、买香烟怎么办?便利商店里能解决大问题!
- 11天!日本史上最长黄金周来了!旅游万金句总结!
- 北川景子&DAIGO缘定1.11 召开记者会宣布结婚
- PIKO‘PPAP’ 洗脑歌登上美国告示牌
- 标签列表
-
- ifneq (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)