[英]Why does aggregate initialization not work anymore since C++20 if a constructor is explicitly defaulted or deleted?
[英]Why C++20 allows defaulted comparison to compile even when it is deleted?
考慮以下代碼:
struct A {
};
struct B {
A a;
bool operator == (const B& other) const = default;
};
clang 給出了一個很好的警告:
警告:顯式默認的相等比較運算符被隱式刪除 [-Wdefaulted-function-deleted] bool operator == (const B& other) const = default;
但我想知道為什么這個代碼甚至被標准接受。 我假設如果有人在他的非模板結構/類中默認operator ==
,他的意圖是永遠不會刪除operator ==
。
但這是 C++ 有一百萬個角落案例,所以可能有充分的理由。 也許不是特殊情況模板?
但是 clang 足夠聰明,不會在此代碼上發出警告......
struct A {
};
template<typename T>
struct TS{
T t;
bool operator == (const TS& other) const = default;
};
int main() {
TS<int> ti;
}
...所以理論上標准可以做同樣的事情。
在模板中,如果可以存在,您可能需要==
,否則不存在。
相同的技術用於復制/移動/分配特殊成員函數; =default;
也可以刪除成員 function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.