首页 > 编程语言 >c++ template

c++ template

时间:2022-10-26 21:57:52浏览次数:43  
标签:std float testclass c++ template public testfunc

template<typename  T, typename  M>

//基本的模板函数和模板类
void testfunc(T a, M b)
{
    std::cout<<a<<b<<std::endl;
}

template<typename  T, typename  N>
class testclass
{
public:
    testclass(T a, N b){}
    ~testclass()= default;

};

int main()
{
    testfunc(3,4);
    testclass<int,float> atest(3,1.5);

}

 

//全特化,意思就是模板失去效果,类型全指定
template<typename  T, typename  M>void testfunc(T a, M b)
{
    std::cout<<a<<b<<std::endl;
}


template<>void testfunc(int a, float  b)
{
    std::cout<<a<<b<<std::endl;
}


template<typename  T, typename  N>class testclass
{
public:
    testclass(T a, N b){}
    ~testclass()= default;
};

template<>  class testclass<int ,float>
{
public:
    testclass(int a, float  b){std::cout<<a<<b<<"全特化构造"<<std::endl;};
    ~testclass() = default;;

};

int main()
{
    testfunc(3,1.2);
    testclass<int,float> a(1,3.5) ;
}

 

//偏特化,以上两种的杂交
template<typename  T, typename  M>void testfunc(T a, M b)
{
    std::cout<<a<<b<<std::endl;
}


template<typename  T>void testfunc(T a, float  b)
{
    std::cout<<a<<b<<std::endl;
}


template<typename  T, typename  N>class testclass
{
public:
    testclass(T a, N b){}
    ~testclass()= default;
};

template<typename  T>  class testclass<T ,float>
{
public:
    testclass(T a, float  b){std::cout<<a<<b<<"全特化构造"<<std::endl;};
    ~testclass() = default;;

};

int main()
{
    testfunc(3,1.2);
    testclass<int,float> a(1,3.5) ;
}

 

标签:std,float,testclass,c++,template,public,testfunc
From: https://www.cnblogs.com/dbnn/p/16830199.html

相关文章

  • C++模板元编程实战 电子书 pdf
    作者:李伟出版社:人民邮电出版社副标题:一个深度学习框架的初步实现 链接:C++模板元编程实战  《C++模板元编程实战:一个深度学习框架的初步实现》以一个深度学......
  • c++当中的引用
    引用在c++中,我们有一种比传递指针更加高效的方式,那就是引用(Reference)。引用类似于windows环境下的快捷方式,通过快捷方式和可执行程序本身都可以运行程序。引用的定义方......
  • unityC++
    2022/10/26:拖动球时,按住shift可以在面对场景的平面内拖动。  网格控制 (点了之后会吸附网格,看起来像一帧一帧移动) 旋转快捷键W。蓝色垂直z轴,绿色垂直Y轴(内层)(外......
  • c++ ##
    #include<iostream>#definenew_fun(parm)print##parm("thisisaprintf\r\n");intmain(){//new_fun(oh);//无法编译通过new_fun(f);getchar();......
  • 【leetcode_C++_字符串_day7】344_反转字符串&541_反转字符串II&&剑指Offer_05_替换空
    344.反转字符串编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组s的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用O(1)......
  • 实验3 数组、指针与现代c++标准库
    实验任务5info.hpp#pragmaonce#include<iostream>#include<string>#include<iomanip>usingnamespacestd;classinfo{public:info(stringname,string......
  • [C++]linux下实现ls()函数遍历目录
    intls(std::stringpath,std::string&ret){DIR*dirp=opendir(path.c_str());if(!dirp){return-1;}structstatst;structdirent*di......
  • C++中#和##
    转自:https://blog.csdn.net/YhL_Leo/article/details/488790931.介绍 C/C++的宏中, #的功能是将其后面的宏参数进行字符串化操作;## 功能是在带参数的宏定义中将两......
  • c++相关的 常用帮助函数集
    ////CreatedbyDangXSon2022/4/27.//#ifndefCPLUSPLUS_PROJECT1_HELPER_H#defineCPLUSPLUS_PROJECT1_HELPER_H#include<iostream>#include<cstring>#inc......
  • C++ #pragma once
    转自:https://www.cnblogs.com/hokyhu/archive/2009/03/30/1425604.html 1.介绍避免同一个文件被include多次,C/C++中有两种方式,一种是#ifndef方式,一种是#pragmaonce方式......