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
}

...补充中

相关推荐
khalil10202 分钟前
代码随想录算法训练营Day-46 动态规划13 | 647. 回文子串、516.最长回文子序列、动态规划总结
数据结构·c++·算法·leetcode·动态规划·回文子串·回文子序列
挨踢ren13 分钟前
单例模式:C++实现与多线程安全
c++·设计模式
skywalk816315 分钟前
Trae生成的中文编程语言关键字(如“定“、“函“、“印“等)需要和标识符之间用 空格 隔开,以确保正确识别
服务器·开发语言·编程
用户8055336980319 分钟前
现代Qt开发教程(新手篇)1.14——日志
c++·qt
红色的小鳄鱼21 分钟前
前端面试js手写
开发语言·前端·javascript
海盗123429 分钟前
C#中的IEqualityComparer<T>使用
开发语言·c#
江公望38 分钟前
Qt QSharedPointer用法,10分钟讲清楚
开发语言·qt
月落归舟44 分钟前
深入理解Java适配器模式,彻底搞懂设计思想
java·开发语言·适配器模式
Mr_pyx1 小时前
【LeetHOT100】二叉树的中序遍历——Java多解法详解
java·开发语言·深度优先
艾莉丝努力练剑1 小时前
【Linux网络】Linux 网络编程入门:TCP Socket 编程(下)
linux·运维·服务器·网络·c++·tcp/ip