7
爲什麼下面的打印出來"A boolean!"
?我意識到有一些奇怪的轉換正在進行,因爲如果我明確地構造一個std :: string我會得到正確的行爲。但爲什麼在以下情況下超載分辨率選擇visitor::operator()(bool)
?的boost ::變種遊客選擇了錯誤的超負荷
#include <boost/variant.hpp>
#include <string>
typedef boost::variant<bool, std::string> type;
struct visitor : public boost::static_visitor<> {
void operator()(bool b) const {
std::cout << "A boolean!" << std::endl;
}
void operator()(const std::string& str) const {
std::cout << "A string!" << std::endl;
}
};
int main(int argc, char* argv[]) {
type t = "I am a string";
t.apply_visitor(visitor());
return 0;
}
我在運行Visual Studio 2012(CTP或不給出了同樣的結果)
FTR的'apply_visitor'成員不是公共接口的一部分。你應該使用獨立版本:'apply_visitor(visitor(),t)'。 – 2013-03-26 12:25:46
謝謝@LucDanton。錯過了,當我閱讀文件 – 2013-03-26 12:43:34