网站首页 > 博客文章 正文
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:简单、单类型参数概念使用缩略记法更好
你 发表评论:
欢迎- 07-07Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- 07-07Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- 07-07Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- 07-07Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- 07-07IT运维必会的30个工具(it运维工具软件)
- 07-07开源项目有你需要的吗?(开源项目什么意思)
- 07-07自动化测试早就跑起来了,为什么测试管理还像在走路?
- 07-07Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- 最近发表
-
- Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- IT运维必会的30个工具(it运维工具软件)
- 开源项目有你需要的吗?(开源项目什么意思)
- 自动化测试早就跑起来了,为什么测试管理还像在走路?
- Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- Cursor 太贵?这套「Cline+OpenRouter+Deepseek+Trae」组合拳更香
- 为什么没人真的用好RAG,坑都在哪里? 谈谈RAG技术架构的演进方向
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (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)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)