c++ - 为什么clang似乎不一致地发出-Wpessimizing-move
问题描述
为什么clang-Wpessimizing-move
为第一个函数发出a,而不是第二个函数?
是因为模板函数“可能会或可能不会”被实例化为“真/假”吗?如果是这样,为什么在模板实例化时没有发出警告true
?
-Wall
用or编译-Wpessimizing-move
。
#include <iostream>
#include <vector>
auto internal_constexpr() {
constexpr bool foo = true;
if constexpr(foo){
std::vector<int> a{1,3,3,4};
return std::move(a);
}else{
return std::vector<int>{1,2,3,4};
}
}
template<bool foo>
auto template_param() {
if constexpr(foo){
std::vector<int> a{1,2,3,4};
return std::move(a);
}else{
return std::vector<int>{1,2,3,4};
}
}
int main(){
auto a = template_param<true>();
auto b = internal_constexpr();
std::cout << a.size() << ", " << b.size();
}
解决方案
推荐阅读
- postgresql - AWS 上的 Postgres 11.8 在 15 分钟后失去连接/终止查询,没有通知或错误
- mysql - 适用于 Oracle 的触发器,我正在尝试生成 MySQL 等价物
- php - PHP FPM 不从 php.ini 加载更改
- java - Android问题加载小部件-logcat中没有错误
- excel - excel中是否有公式来查找row1中的两个字母单元格是否存在于row2和3中?
- java - Java - Ecipe IDE 错误 - 引导层初始化期间发生错误
- r - 在 R 中遇到问题
- javascript - 如何对数组对象进行分组以获得所需的数据格式?
- r - 用每条/组的观察数注释 ggplot boxplot facets
- python - 从 sys.path 中删除路径