c++11强化知识点

2020-11-15  本文已影响0人  拉普拉斯妖kk

初始化列表(std::initializer_list)

#include <iostream>
#include <initializer_list>
#include <vector>

using namespace std;

class InitClass {
public:
    InitClass(initializer_list<int> list) {
        for (int l : list)
            initializer_list_.emplace_back(l);
    }

    void PrintInit() {
        for (int l : initializer_list_)
            cout << l << endl;
    }

    void Print(initializer_list<int> list) {
        for (int l : list)
            cout << l << endl;
    }

private:
    vector<int> initializer_list_;
};

struct A {
    double a;
    int b;
};
struct B {
    B(int a, double b): a_(a), b_(b) {}
private:
    int a_;
    double b_;
};


int main()
{
    // 使用初始化列表初始化类对象
    InitClass i = {1, 2, 3, 4, 5};
    i.PrintInit();
    cout << endl;
    // 初始化列表做函数参数
    i.Print({1, 2, 3});
    // 使用初始化列表初始化POD数据
    vector<int> v = {1, 2, 3, 4};
    A a {1.1, 1};
    B b {2, 2.2};
    
    return 0;
}

变长参数模板(typename... Args)

#include <iostream>
template<typename T>
void printf(T value) {
    std::cout << value << std::endl;
}
template<typename T, typename... Args>
void printf(T value, Args... args) {
    std::cout << value << std::endl;
    printf(args...);
}
int main() {
    printf(1, 2, "123", 1.1);
    return 0;
}
// 编译这个代码需要开启 -std=c++14
#include <iostream>
template<typename T, typename... Args>
auto print(T value, Args... args) {
    std::cout << value << std::endl;
    return std::initializer_list<T>{([&] {
        std::cout << args << std::endl;
    }(), value)...};
}
int main() {
    print(1, 2.1, "123");
    return 0;
}

强类型枚举(enum class)

#include <iostream>
enum class new_enum : unsigned int {
    value1,
    value2,
    value3 = 888,
    value4 = 888
};

template<typename T>
std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e) {
    return stream << static_cast<typename std::underlying_type<T>::type>(e);
}

template<typename T>
auto to_underlying_type(const T& e) {
    return static_cast<typename std::underlying_type<T>::type>(e);
}

int main() {
    if (new_enum::value3 == new_enum::value4 && 888 == to_underlying_type(new_enum::value3))
    {
         std::cout << new_enum::value3 << std::endl;
    }
    return 0;
}

函数对象包装器(std::function)

#include <iostream>
#include <memory>

class A {
public:
    using CallBack = std::function<void()>;

    void SetCallBack(CallBack cb) { call_back_ = cb; }
    void CallCallBack() {
        if (call_back_)
        {
            call_back_();
        }
        
    }

private:
    CallBack call_back_ = nullptr;
};

class B {
public:
    B() {
        a = std::make_shared<A>();
        a->SetCallBack([](){
            std::cout << "call A cb in B" << std::endl;
        });
        a->CallCallBack();
    }

private:
    std::shared_ptr<A> a = nullptr;
};

int main() {
    B b;
}

智能指针初始化(make_unique)

#include <memory>

template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args ) {
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

int main() {
    std::unique_ptr<int> p = make_unique<int>(1);
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读