没有参数的函数模板

版权声明:版权所有,转载请注明出处 https://blog.csdn.net/songchuwang1868/article/details/83024484

一般而言,函数模版需要通过参数进行类型推断。没有参数的函数模板必须通过显示实例化来使用。

使用场景:any类型的get函数

示例:

#include<iostream>
template <typename T>
T fun();

template<>
int fun()
{
  return 9;
}

template<>
double fun()
{
  return 3.4;
}

int main()
{
  std::cout<<fun<int>()<<std::endl;//显式实例化
  std::cout<<fun<double>()<<std::endl;
  return 0;
}

猜你喜欢

转载自blog.csdn.net/songchuwang1868/article/details/83024484
今日推荐