首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
gV0拜客生活常识网

Here is an example where two threads execute the same function and guard access to std::cout object by locking and unlocking mutexes:gV0拜客生活常识网

#include 
#include 
#include 
#include 
std::mutex m; // will guard std::cout
void myfunction(const std::string& param)
{
    for (int i = 0; i < 10; i++)
    {
        m.lock();
        std::cout << "Executing function from a " << param << '
';
        m.unlock();
    }
}
int main()
{
    std::thread t1{ myfunction, "Thread 1" };
    std::thread t2{ myfunctiosn, "Thread 2" };
    t1.join();
    t2.join();
}

We can forget to unlock the mutex manually. A better approach is to use the std::lock_guard function instead. It locks the mutex, and once it goes out of scope, it automatically unlocks the mutex. Example:gV0拜客生活常识网

#include 
#include 
#include 
#include 
std::mutex m; // will guard std::cout
void myfunction(const std::string& param)
{
    for (int i = 0; i < 10; i++)
    {
        std::lock_guard lg(m);
        std::cout << "Executing function from a " << param << '
';
    } // lock_guard goes out of scope here and unlocks the mutex
}
int main()
{
    std::thread t1{ myfunction, "Thread 1" };
    std::thread t2{ myfunction, "Thread 2" };
    t1.join();
    t2.join();
}

11.14 Deleted and Defaulted Functions

If we do not supply a default constructor, the compiler will generate one for us so that we can write:gV0拜客生活常识网

class MyClass
{
};
int main()
{
    MyClass o; // OK, there is an implicitly defined default constructor
}

However, in certain situations, the default constructor will not be implicitly generated. For example, when we define a copy constructor for our class, the default constructor is implicitly deleted. Example:gV0拜客生活常识网

#include 
class MyClass
{
public:
    MyClass(const MyClass& other)
    {
        std::cout << "Copy constructor invoked.";
    }
};
int main()
{
    MyClass o; // Error, there is no default constructor
}

To force the instantiation of a default, compiler-generated constructor, we provide the =default specifier in its declaration. Example:gV0拜客生活常识网

#include 
class MyClass
{
public:
    MyClass() = default; // defaulted member function
    MyClass(const MyClass& other)
    {
        std::cout << "Copy constructor invoked.";
    }
};
int main()
{
    MyClass o; // Now OK, the defaulted default constructor is there
    MyClass o2 = o; // Invoking the copy constructor
}

The =default specifier, when used on a member function, means: whatever the language rules, I want this default member function to be there. I do not want it to be implicitly disabled.gV0拜客生活常识网

Similarly, if we want to disable a member function from appearing, we use the =delete specifier. To disable the copy constructor and copy assignment, we would write:gV0拜客生活常识网

#include 
class MyClass
{
public:
    MyClass()
    {
        std::cout << "Default constructor invoked.";
    }
    MyClass(const MyClass& other) = delete; // delete the copy constructor
    MyClass& operator=(const MyClass& other) = delete; // delete the copy // assignment operator
};
int main()
{
    MyClass o; // OK
    MyClass o2 = o; // Error, a call to deleted copy constructor
    MyClass o3;
    o3 = o; // Error, a call to deleted copy assignment operator
}

These specifiers are mostly used in situations where we want to:gV0拜客生活常识网

a. force or the instantiation of implicitly defined member functions such as constructors and assignment operators, when we use the =default; expressiongV0拜客生活常识网

b. disable the instantiation of implicitly defined member functions using the =delete; expressiongV0拜客生活常识网

These expressions can also be used on other functions as well.gV0拜客生活常识网

115 Type Aliases

A type alias is a user-provided name for the existing type. If we want to use a different name for the existing type, we write:

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。