私はexplicitキャストオペレーターのための新しいもので遊んでいます。あなたが次のようなものを書く場合
struct Data {
explicit operator string();
};
誤ってに変換することはできませDataんstring。dargetデータ型boolは例外です。場合によっては、explicit-- contextualconversionとマークされていても暗黙的な変換が許可されます。if(...)したがって、次の例でこのデータ型を使用できます。
struct Ok {
explicit operator bool(); // allowed in if(...) anyway
};
「25.4。(2)並べ替えと関連操作」の段落は、同様の標準的なコンテナCompareのファンクターにもこれを可能にするようです。しかし、gcc-4.7.0での試行は失敗し、それが私の誤解なのか、gccのバグなのかは確かですか?set
#include <set>
struct YesNo { // Return value type of Comperator
int val_;
explicit YesNo(int y) : val_{y} {}
/* explicit */ operator bool() { return val_!=0; }
};
static const YesNo yes{1};
static const YesNo no{0};
struct LessYesNo { // Comperator with special return values
YesNo operator()(int a, int b) const {
return a<b ? yes : no;
}
};
int main() {
std::set<int,LessYesNo> data {2,3,4,1,2};
}
例がコンパイルされるexplicit前に。operator bool()そして、「25.4。(2)」についての私の理解は、これも`explicitでコンパイルする必要があるということです。
変換setも機能するはずだというStdを正しく理解しましたか?explicit boolそして、これはgccのバグである可能性がありますか、それとも私は何か間違っていることを理解しましたか?