c++ templates常用函数

说明

c++ templates学习中会遇到大量的模版常用函数,书上不会详细介绍,查看一个之后要永久记录一段时间之后再看看,这里总结一下。

undeclared();

undeclared();//若undeclared();未定义,则在第一阶段编译时报错

undeclared(t);//若t未知,则在第二编译阶段报错

std::decay<T>

在<type_traits>定义,将T类型转换为它的衰变类型。例如去const、去&、去易变性限定符、数组变为*。

cpp 复制代码
#include <type_traits>
#include <iostream>
template<typename T1,typename T2>
auto max(T1 a, T2 b)->typename std::decay<decltype(true ? a : b)>::type
{
    return a > b ? a : b;
}
template<typename T>
void fun(T&& t)
{
    //我们希望创建与参数同类型的,而不是引用
    typedef typename std::decay<T>::type U;
    U u;
    //x与t的原始类型相同
    typename std::decay<T>::type x = t;
}
enum Enuma
{
    type1,
    type2
};
class Type{};
void main()
{
    //类型都是int
    std::decay<int&&>::type a;
    std::decay<int&>::type b;
    std::decay<const int>::type c;
    std::decay<volatile int>::type d;
    //降级为int*
    std::decay<int[10]>::type e;
    //void(*)()
    std::decay<void()>::type f;
    //enum、class类型
    std::decay<const Enuma>::type g;
    std::decay<const Type&>::type h;
    a = 1;
}

std::remove_reference_t<T> //移除引用

std::remove_cv_t<T>//移除const或volatile

auto初始化是衰变的

std::common_type<typename... T>

函数返回值的类型可以转换的类型。

确定所有类型 T... 的共用类型,即所有 T... 都能隐式转换到的类型。若这种类型(根据后述规则确定)存在,则成员 type 指名该类型。否则,无成员 type 。

cpp 复制代码
#include <type_traits>
#include <iostream>
template<typename T,typename U>
typename std::common_type<T, U>::type max(T t, U u)
{
    return t > u ? t : u;
}
enum Enuma
{
    type1,
    type2
};
class Type{};
void main()
{
    auto a = max(1, 2.1);//dobule
    auto b = max(2, 1.1);//double
    std::cout<< max(1, 2.1)<<std::endl;//2.1
    std::cout<< max(2, 1.1);//2
    a = 1;
}
cpp 复制代码
#include <iostream>
#include <type_traits>

struct Base {};
struct Derived : Base {};

int main(int argc, char** argv)
{
    {
        typedef std::common_type<char, short, int>::type A;        
        typedef std::common_type<float, double>::type B;           
        typedef std::common_type<Derived, Base>::type C;           
        typedef std::common_type<Derived*, Base*>::type D;         
        typedef std::common_type<const int, volatile int>::type E; 

        std::cout << std::boolalpha;
        std::cout << "typedefs of int:" << std::endl;
        std::cout << "A: " << std::is_same<int, A>::value << std::endl;
        std::cout << "B: " << std::is_same<int, B>::value << std::endl;
        std::cout << "C: " << std::is_same<int, C>::value << std::endl;
        std::cout << "D: " << std::is_same<int, D>::value << std::endl;
        std::cout << "E: " << std::is_same<int, E>::value << std::endl;
    }
    {
        typedef std::common_type_t<char, short, int> A;        
        typedef std::common_type_t<float, double> B;           
        typedef std::common_type_t<Derived, Base> C;           
        typedef std::common_type_t<Derived*, Base*> D;         
        typedef std::common_type_t<const int, volatile int> E; 

        std::cout << std::boolalpha;
        std::cout << "typedefs of int:" << std::endl;
        std::cout << "A: " << std::is_same_v<int, A> << std::endl;
        std::cout << "B: " << std::is_same_v<int, B> << std::endl;
        std::cout << "C: " << std::is_same_v<int, C> << std::endl;
        std::cout << "D: " << std::is_same_v<int, D> << std::endl;
        std::cout << "E: " << std::is_same_v<int, E> << std::endl;
    }

    return 0;
}

输出:
typedefs of int:
A: true
B: false
C: false
D: false
E: true
typedefs of int:
A: true
B: false
C: false
D: false
E: true

std::is_same<T1,T2>

如果 TU 指名同一类型(考虑 const/volatile 限定),那么成员常量 value 等于 true。

cpp 复制代码
    auto b=std::is_same<int, const int>::value;//false//is_same<>不降级
    auto b1 = std::is_same<int,std::uint8_t>::value;//false//类型int不相同

constexpr

提供在编译时计算某些值的能力。

cpp 复制代码
template<typename T1,typename T2>

constexpr auto max(T1 a,T2 b)

{

        return a>b?a:b;

}

int a[::max(sizeof(int,1000u)];

std::is_default_constructible<T>

T是否有默认的构造函数。

cpp 复制代码
#include <type_traits>
class A {
    
};
class B {
    B() = delete;
};
int main(int argc, char** argv) {
    bool b = std::is_default_constructible<A>::value;//true
    bool b1 = std::is_default_constructible<B>::value;//false
}

std::add_const<T>, std::add_cv<T>, std::add_volatile<T>

增加const, const volatile, volatile.

cpp 复制代码
#include <iostream>
#include <type_traits>

struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
    void m() volatile { std::cout << "Volatile\n"; }
    void m() const volatile { std::cout << "Const-volatile\n"; }
};

int main()
{
    foo{}.m();//Non-cv
    std::add_const<foo>::type{}.m();//Const
    std::add_volatile<foo>::type{}.m();//Volatile
    std::add_cv<foo>::type{}.m();//Const - volatile
}

...补充中

相关推荐
Xxxx. .Xxxx14 分钟前
C语言程序设计实验与习题指导 (第4版 )课后题-第二章+第三章
java·c语言·开发语言
逸狼15 分钟前
【JavaEE初阶】多线程6(线程池\定时器)
java·开发语言·算法
tan77º1 小时前
【C++】异常
c++·算法
薛文旺2 小时前
c++可视化打印树
开发语言·c++
计算机学姐2 小时前
基于python+django+vue的旅游网站系统
开发语言·vue.js·python·mysql·django·旅游·web3.py
qq_278063712 小时前
css scrollbar-width: none 隐藏默认滚动条
开发语言·前端·javascript
DogDaoDao2 小时前
Windows 环境下 vscode 配置 C/C++ 环境
c语言·c++·windows·vscode·gcc·mingw-w64
q4725994512 小时前
OpenGL 原生库6 坐标系统
c++
Beginner_bml2 小时前
结构体---C语言
c语言·开发语言·数据结构
程序员小羊!2 小时前
Python语言基础教程(下)4.0
开发语言·python