小编Son*_*nny的帖子

如何根据条件推断可以返回类型的函数的类型?

我有一个模板函数,它接受两种数据类型 - int和double并返回一个较小的数据,现在,我如何推断出这个函数返回的类型?现在,我正在丢失小数点后的部分.

#include <iostream>

using namespace std;

template <class First, class Second>
First smaller(First a, Second b){
   return (a < b ? a : b);
}

int main () {
   int x = 100;
   double y = 15.5;
   cout<< smaller (x,y) << endl;

}
Run Code Online (Sandbox Code Playgroud)

c++ templates casting c++11

3
推荐指数
1
解决办法
128
查看次数

标签 统计

c++ ×1

c++11 ×1

casting ×1

templates ×1