首页 > 编程语言 >C++模板例子

C++模板例子

时间:2024-01-11 16:14:18浏览次数:39  
标签:std Container void C++ 例子 template Test class 模板

title: "C++模板例子"
date: 2023-11-02T01:05:25+08:00
tags: ["C++"]
categories: []
draft: false
#include <vector>
#include <type_traits>
using namespace std;

class AA {};
class BB {};


class Test {
public:
    template <class T, template <class> class Container, std::enable_if_t<std::is_same_v<T, int>> * = nullptr>
    void f1(Container<T> &ret);

    template<template<class> class Container>
    void f2(Container<AA> *);

    template<template<class> class Container>
    void f3(Container<BB> *);
};


template <class T, template <class> class Container, std::enable_if_t<std::is_same_v<T, int>> * = nullptr>
void Test::f1(Container<T> &ret)
{
    cout << __func__ << std::endl;
    ret.push_back(1);
//    if constexpr (std::is_same_v<int, T>) {
//        std::cout << "inst" << std::endl;
//    }
}

template <template<class> class Container>
void Test::f2(Container<AA> *c)
{
    std::cout << __func__ << std::endl;
}

template <template<class> class Container>
void Test::f3(Container<BB> *c)
{
    std::cout << __func__ << std::endl;
}

struct X {enum { value1 = false, value2 = true };};
template<class T, std::enable_if_t<T::value2, int> = 0>
void func() {
    std::cout << __func__ << std::endl;
}


int main()
{
    vector<int> vec(10);

    Test *test = new Test;
    test->template f1<int, std::vector>(vec);
    test->template f2<std::vector>(nullptr);
    test->template f3<std::vector>(nullptr);

    func<X>();
    return 0;
}

标签:std,Container,void,C++,例子,template,Test,class,模板
From: https://www.cnblogs.com/devin1024/p/17958788

相关文章

  • C++中 统计程序执行耗时
    C++程序有时需要统计一段代码的执行消耗时间,可以通过类chrono库来进行计算。该库中常常使用两个类来进行计算时间:std::chrono::steady_clock:表示稳定的时钟std::chrono::system_clock:表示当前系统时钟代码如下#include<chrono>usingnamespacestd::chrono;doubleG......
  • java使用 template模板ftl 含有图片的生成数据
    点击查看代码/***Base64编码.*/publicstaticStringbase64Encode(byte[]input){BASE64Encoderencoder=newBASE64Encoder();returnencoder.encode(input);}@OverridepublicvoidprintStudentRxdjb(StudentRxdj......
  • 网络安全等级保护等级测评方案模板
    ......
  • 【C++/Qt】QLCDNumber-电子时钟实战
    头文件:#ifndefDIGITALCLOCK_H#defineDIGITALCLOCK_H#include<QLCDNumber>classdigitalClock:publicQLCDNumber{Q_OBJECTpublic:digitalClock(QWidget*parent=0);protected:voidmousePressEvent(QMouseEvent*event);//鼠标点击事件void......
  • Python Flask html 模板的继承
    前言全局说明一、安装flask模块官方源:pip3installflask==2.3.2国内源:pip3installflask==2.3.2-ihttp://pypi.douban.com/simple/--trusted-hostpypi.douban.com以上二选一,哪个安装快用哪个flask安装时间2023-11更多国内源:https://www.cnblogs.com/wutou......
  • 【设计模式】模板方法模式——模板方法模式在JDK源码中的应用
    模板方法模式在JDK源码里最典型的实现就是AQSAbstractQueuedSynchronizerAQS(AbstractQueuedSynchronizer)的部分代码如下:publicabstractclassAbstractQueuedSynchronizerextendsAbstractOwnableSynchronizerimplementsjava.io.Serializable{//……代码......
  • 【C/C++】知识点笔记
    1-联合体内嵌结构体初始化赋值union{struct{inti;floatf;char*p;};into;}obj3={1,2.2,"sk",4,9};printf("structinlayunion:%d,%f,%s,%d\n",obj3.i,obj3.f,obj3.p,obj3.o);输出:structin......
  • C++ 拷贝构造函数
    拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:通过使用另一个同类型的对象来初始化新创建的对象。复制对象把它作为参数传递给函数。复制对象,并从函数返回这个对象。如果在类中没有定义拷......
  • agx orin 使用 sdm 刷机后,vscode 使用 C++ 版本的 opencv, 出现红色的波浪线,但是程序
    原因:vscode没有链接好opencv的头文件先找到opencv头文件的位置:sudofind/-iname"opencv"/usr/include/opencv4/usr/include/opencv4/opencv2解决:ctril+sheft+p:打开:c_cpp_properties.json,写入:"includePath":["${workspaceFo......
  • C++设计模式05 —— 装饰模式
    装饰模式过度的使用继承使得派生的子类过多,代码重复度很高,复用性变差。实现加密文件流、加密网络流、加密内存流、缓冲文件流、缓冲网络流、缓冲内存流。如果我们创建一个流基类,然后创建文件流继承流基类,最后创建加密文件流继承文件流、创建缓冲文件流继承文件流。如果这......