返回类型后置:使用"->"符号,在函数名和参数列表后面指定返回类型。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <map>

int func(int, int);
auto func2(int, int)->int; // 指定返回类型

template<typename T1, typename T2>
auto sum(const T1 &t1, const T2 &t2)->decltype(t1+t2) // 指定返回类型
{
    return t1 + t2;
}

template<typename T1, typename T2>
auto mul(const T1 &t1, const T2 &t2)->decltype(t1*t2) // 指定返回类型
{
    return t1 * t2;
}

void mytest()
{
    auto a = 3;
    auto b = 4L;
    auto pi = 3.14f;

    auto c = mul(sum(a, b), pi);
    std::cout << c << std::endl; // 21.98

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

相关文章:

  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
猜你喜欢
  • 2021-10-28
  • 2021-04-13
  • 2022-01-14
  • 2021-08-27
  • 2023-01-10
  • 2022-02-06
  • 2022-12-23
相关资源
相似解决方案