首页 > 解决方案 > 为什么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();
}

标签: c++clang++

解决方案


推荐阅读