《C++20编程技巧》笔记2

运行在VS2022,内容如下。

目录

8.C++20指定初始化

指定初始化可以在初始化某个成员时跳过其他成员,从而提供更灵活的初始化方式。

9.模板函数不能用特定类型编译时

使用模板偏特化来创建模板重载。

在本例中首选添加++和<<运算符支持。

10.创建单例

将构造函数设为私有,防止外部直接创建对象,并且删除拷贝构造函数和赋值运算符。提供一个公共的静态成员函数来获取唯一实例。使用静态局部变量确保只初始化一次。

使用模板来创建一个Singleton基类。

11.在编译时计算值

模板元编程利用C++模板编译器的优势在编译时计算数值,可以节省运行时的性能。

12.C++20concept

概念可以用于对模板参数进行约束和限制。在编译时对泛型代码进行更严格的类型检查,提高代码的可读性和可维护性(#include)。


8.C++20指定初始化

指定初始化可以在初始化某个成员时跳过其他成员,从而提供更灵活的初始化方式。

【注意】指定初始化仅适用于结构体和类的成员初始化。

【注意】初始化顺序要和声明顺序匹配,否则报错。

#include<iostream>
using namespace std;

struct Point {
    int x;
    int y;
    int z;
};

int main()
{
    Point p1 = { .x = 1, .z = 3 , };
    cout << p1.x << ", " << p1.y << ", " << p1.z << endl;

    return 0;
}

9.模板函数不能用特定类型编译时

使用模板偏特化来创建模板重载。

#include<iostream>
using namespace std;

class MyClass
{
private:
    int m_value;

public:
    MyClass():m_value(0){}
    MyClass(int value):m_value(value){}
    MyClass(int num1, int num2) :m_value(num1 + num2) {}

public:
    int getValue() const{ return m_value; }
};

template<typename T>
T Add(const T& a, const T& b)
{
    return a + b;
}

template<>
MyClass Add(const MyClass& a, const MyClass& b)
{
    return MyClass(a.getValue(), b.getValue());
}

template<typename T>
void print(const T& value1, const T& value2, const T& result)
{
     cout << value1 << "+" << value2 << "=" << result << endl;
}

template<>
void print(const MyClass& value1, const MyClass& value2, const MyClass& result)
{
    cout << value1.getValue() << "+" << value2.getValue() << "=" << result.getValue() << endl;
}

int main()
{
    const MyClass num1(2);
    const MyClass num2(5);
    const MyClass res(Add(num1, num2));
    print(num1, num2, res);

    return 0;
}

在本例中首选添加++和<<运算符支持。

#include<iostream>
using namespace std;

class MyClass
{
    friend ostream& operator<<(ostream& os, const MyClass& myClass);

private:
    int m_value;

public:
    MyClass():m_value(0){}
    MyClass(int value):m_value(value){}
    MyClass(int num1, int num2) :m_value(num1 + num2) {}

    MyClass operator+(const MyClass& other) const
    {
        return m_value + other.m_value;
    }
};

ostream& operator<<(ostream& os, const MyClass& myClass)
{
    os << myClass.m_value;
    return os;
}


template<typename T>
T Add(const T& a, const T& b)
{
    return a + b;
}

template<typename T>
void print(const T& value1, const T& value2, const T& result)
{
    cout << value1 << "+" << value2 << "=" << result << endl;
}


int main()
{
    const MyClass num1(2);
    const MyClass num2(5);
    const MyClass res(Add(num1, num2));
    print(num1, num2, res);

    return 0;
}

10.创建单例

将构造函数设为私有,防止外部直接创建对象,并且删除拷贝构造函数和赋值运算符。提供一个公共的静态成员函数来获取唯一实例。使用静态局部变量确保只初始化一次。

#include<iostream>
using namespace std;

class MySingleton
{
private:
    int number;

private:
    MySingleton(): number(100){}

    MySingleton(const MySingleton&) = delete;
    MySingleton& operator=(const MySingleton&) = delete;

public:
    static MySingleton& getInstance() {
        static MySingleton instance;
        return instance;
    }

    void print() { cout << "singleton successfully printing!" << endl; }

    int getNumber() { return number; }
};

int main()
{
    MySingleton& instance = MySingleton::getInstance();
    instance.print();
    cout << instance.getNumber() << endl;

    return 0;
}

使用模板来创建一个Singleton基类。

#include<iostream>
using namespace std;

template <typename T>
class Singleton 
{
protected:
    Singleton() {};
    virtual ~Singleton() {};
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

public:
    static T& getInstance() {
        static T instance;
        return instance;
    }
};

class MySingleton : public Singleton<MySingleton>
{
private:
    int number;

private:
    MySingleton() : number(100) {}
    friend class Singleton<MySingleton>;

public:
    void print() { cout << "singleton successfully printing!" << endl; }
    int getNumber() { return number; }
};

int main()
{
    MySingleton& instance = MySingleton::getInstance();
    instance.print();
    cout << instance.getNumber() << endl;

    return 0;
}

11.在编译时计算值

模板元编程利用C++模板编译器的优势在编译时计算数值,可以节省运行时的性能。

#include<iostream>
using namespace std;

//阶乘的普通函数
int factorial(int n) {
    if (n == 0) 
        return 1;
    return  n * factorial(n - 1);
}

//阶乘的模板元程序
template <int N>
struct Factorial 
{
    static const int value = N * Factorial<N - 1>::value;
};

template <>
struct Factorial<0> 
{
    static const int value = 1;
};


int main() 
{
    //编译器计算
    cout << Factorial<5>::value << endl;
    //运行时计算
    cout << factorial(5) << endl;
    return 0;
}

12.C++20concept

概念可以用于对模板参数进行约束和限制。在编译时对泛型代码进行更严格的类型检查,提高代码的可读性和可维护性(#include<concepts>)。

#include<iostream>
#include<concepts>
using namespace std;

//约束为整数类型
template<typename T>
concept Integral = is_integral_v<T>;

template<Integral T>
void print(T value)
{
    cout << value << endl;
}

int main() 
{
    short num = 2;
    print(num);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值