错误的自动扣除 c++14

Wrong auto deduction c++14

本文关键字:c++14 错误      更新时间:2023-10-16

我对"auto"返回类型函数有一种奇怪的行为。有人知道为什么第二个函数调用返回 int 而不是双精度吗?

编译器版本: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10( 5.4.0 20160609

#include <string>
#include <iostream>
double operator+(const int& a,const std::string& b){
return 3.4;
}
auto f(){
return 3.4;
}
auto sum(auto a, auto b){
return a + b;
}
int main(){
std::cout<< sum(1.0, std::string("hello")) <<std::endl;
std::cout<< sum(1, std::string("hello")) <<std::endl;
std::cout<< f() << std::endl;
}
//3.4
//3
//3.4

首先,自动函数参数不是标准C++。这是一个gcc扩展,期待标准委员会的一些工作。有一次有人谈论允许这种语法与概念的引入。我不知道情况是否仍然如此。

至于奇怪的行为,它看起来只是一个编译器错误,修复在gcc 5.5.0和gcc 6.1.0之间