このコード:なぜ{}が最初にstd :: nullptr_tに変換されるのですか?
#include <iostream>
#include <vector>
using namespace std;
void dump(const std::string& s) {
cout << s << endl;
}
class T {
public:
T() {
dump("default ctor");
}
T(std::nullptr_t) {
dump("ctor from nullptr_t");
}
T(const T&) {
dump("copy ctor");
}
T& operator=(const T&) {
dump("copy operator=");
return *this;
}
T& operator=(std::nullptr_t) {
dump("operator=(std::nullptr_t)");
return *this;
}
T& operator=(const std::vector<int>&) {
dump("operator=(vector)");
return *this;
}
};
int main() {
T t0;
t0 = {};
return 0;
}
default ctor
operator=(std::nullptr_t)
std::nullptr_t
とoperator=
を選択した理由は? {}
はuser-defined conversion sequenceにつながる、両方の#1と#2のために
operator=(T const&)
operator=(std::vector<int> const&)
operator=(std::nullptr_t)
:
'{}'と 'std :: nullptr_t'は標準変換(実際には[over.ics.list]というアイデンティティ変換)ですが、他の変換はユーザ定義ですユーザー定義型。 –
通常、タイプ 'T'が表示されるときはテンプレートパラメータです。これは1秒間私を混乱させた。 –