Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
44
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

1.7 Afternotes

23

struct Base { };

struct Child : Base { int i;

bool operator==(const Child& other) const = default;

};

 

 

 

 

Child

c1,

c2;

 

...

 

 

 

 

c1

==

c2;

 

// ERROR

As a consequence, you also have to provide a defaulted operator == in the base class. However, if you do not want the base class to provide operator == for the public, the obvious approach is to add a protected defaulted operator == in the base class:

struct Base { protected:

bool operator==(const Base& other) const = default;

};

struct Child : Base { int i;

bool operator==(const Child& other) const = default;

};

However, in this case, the defaulted comparisons for the derived class does not work. It is rejected by the current compilers implementing the specified behavior of defaulted operators that is too strict here. This hopefully will be fixed soon (see http://wg21.link/cwg2568).

As a workaround, you have to implement the derived operator yourself.

1.7Afternotes

The request for implicitly defined comparison operators was first proposed by Oleg Smolsky in http:// wg21.link/n3950. The issue was then raised again by Bjarne Stroustrup in http://wg21.link/n4175. However, the final proposed wording, formulated by Jens Maurer in http://wg21.link/p0221r2, was ultimately rejected because it was an opt-out feature (thus, existing types would automatically have comparison operators unless they declared that they did not want this). Various proposals were then considered, which Herb Sutter brought together in http://wg21.link/p0515r0.

The finally accepted wording was formulated by Herb Sutter, Jens Maurer, and Walter E. Brown in http: //wg21.link/p0515r3 and http://wg21.link/p0768r1. However, significant modifications were accepted as formulated by Barry Revzin in http://wg21.link/p1185r2, http://wg21.link/p1186r3, http://wg21.link/p1614r2, and http://wg21.link/p1630r1.

24

This page is intentionally left blank