C++11: 一个demo包含15个C++11标准用法

1.64bit system and g++ 4.7.3
2.转载:https://zhuanlan.zhihu.com/p/36758856?utm_source=wechat_session&utm_medium=social&utm_oi=812025187762589696
3.代码

/*
    Copyright 2019 [email protected]
    License none
    Author [email protected]
    This is demo for C++11
    use: g++ c11.cpp -std=c++11
*/

// #include "my_h.h"
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <functional>
// #include "other_h.h"

using std::cout;
using std::wcout;
using std::endl;
using std::string;
using std::vector;
using std::function;

namespace mynamespace {

enum class APPLE {
    APPLE_RED = 0,
    APPLE_BLUE
};
enum class ORANGE{
    ORANGE_RED = 0,
    ORANGE_BLUE
};

void enum_class()
{
    /* 6.enum class: more strict and need point namespace*/
    APPLE apple_red = APPLE::APPLE_RED;
    ORANGE orange_red = ORANGE::ORANGE_RED;
    // if (apple_red != orange_red) {   // compile error: different type
    //     cout << "apple_red != orange_red" << endl;
    // }
}

constexpr int get_buffer_size(int i)    // 13 constexpr equal define
{
    return i * i * i;
}

class C11_DEMO  // 10.1 add final: mean not allow inheritance
{
public:
    C11_DEMO() = default;   // 11 default constructor create by complier
    C11_DEMO(double) = delete;  // 12 delete hidden way

    C11_DEMO(string name, int age) : C11_DEMO()     // 8.2 use default constructor
    {
        this->m_name = name;
        this->m_age = age;
    }

    virtual void PrintInfo(int i)
    {
        cout << "printf(int)" << endl;
        cout << "name: " << m_name << endl;
        cout << "age: " << m_age << endl;
        cout << "id: " << m_id << endl;
    }

    virtual void PrintInfo(char *p)
    {
        cout << "printf(char *)" << endl;
        cout << "name: " << m_name << endl;
        cout << "age: " << m_age << endl;
        cout << "id: " << m_id << endl;
    }

    virtual void show() final   // 10.2 add final: mean not allow override
    {
        cout << "show()" << endl;
    }

private:
    string m_name;
    int m_age;
    int m_id = 5;       // 8.1 default value
};

class C11_DEMO_SON : public C11_DEMO
{
public:
    explicit C11_DEMO_SON();

    // virtual void PrintInfo(float i) override    // 9.1 compile error: NO allow override
    // {
    //     cout << "C11_DEMO_SON PrintInfo(float i)";
    // }

    // virtual void show()   // 10.2 father final: mean not allow override
    // {
    //     cout << "show()" << endl;
    // }

};

};  // namespace mynamespace

/* other file */
using namespace mynamespace;

int main ()
{
    /* 7.static_assert: compile running not in code running */
    static_assert(sizeof(long) == 8, "64bit system");
    // static_assert(sizeof(void *) == 4, "64bit system");  // compile error

    /* 1.Initializer list: not need push_back(1) */
    vector<int> m_vec = {1, 2, 3};

    /* 13 constexpr */
    int array[get_buffer_size(2)];

    /* 2.Uniform Initialization */
    C11_DEMO d1 = {"demo1", 1};
    d1.PrintInfo(0);

    /* 12.delete hidden way*/
    // C11_DEMO d2(1.1);    // error: already delete

    /* 3.auto type, 4.foreach */
    for (auto &i: m_vec) {      // can change m_vec value by add &
        i++;
    }
    for (auto j: m_vec) {
        /* 5.nullptr */
        d1.PrintInfo(nullptr);
        cout << "m_vec: " << j << endl;
    }

    /* 14 new string */
    const char *b = u8"utf8-string";
    const char16_t *b1 = u"utf16-string";
    const char32_t *b2 = U"utf32-string";

    cout << "c++11 utf8-string: " << b << endl;
    wcout << "c++11 utf16_t-string: " << reinterpret_cast<const wchar_t *>(b1) << endl;
    wcout << "c++11 utf32_t-string: " << reinterpret_cast<const wchar_t *>(b2) << endl;
    cout << "char16_t: " << sizeof(char16_t) << endl;
    cout << "char32_t: " << sizeof(char32_t) << endl;

    /* 15 lambda function: Small and one-time function */
    // https://www.cnblogs.com/DswCnblog/p/5629165.html
    function<int(int, int)> f1 = [](int x, int y) {return x * y;};
    cout << "x * y = " << f1(2, 3) << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hu_yilang/article/details/87914804