我想測試像下面這樣一個簡單的事情:的boost ::變種和運營商<<重載
#include <iostream>
#include <boost/variant.hpp>
template<typename T1,typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1,T2>& dt){
os << dt.first << dt.second;
return os;
}
int main(){
boost::variant<int, std::pair<int,int>, bool> v;
v = std::pair<int,int>(3,3);
std::cout << v << std::endl;
}
這實際上應該工作,因爲正常的類型,如int, double
等等,它編譯。 boost::variant
有一個打印機vistor,它在內部用來輸出內容到流中。 其實這編譯失敗,但我真的不知道的問題:
的代碼失敗在這裏:在variant_io.hpp
template <typename OStream>
class printer
: public boost::static_visitor<>
{
private: // representation
OStream& out_;
public: // structors
explicit printer(OStream& out)
: out_(out)
{
}
public: // visitor interface
template <typename T>
void operator()(const T& operand) const
{
out_ << operand; // HEEEEEEERRRRREE!!!!!!!!!!!!
}
private:
printer& operator=(const printer&);
};
隨着消息:
/usr/local/include/boost/variant/detail/variant_io.hpp|64|error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
有人知道我做錯了什麼,爲什麼?
非常感謝!
這不是我們需要知道的能夠診斷問題的地方。當模板定義出現問題時,編譯器會打印一個擴展鏈。我們需要完整的輸出來告訴你發生了什麼。 –
由於錯誤消息似乎來自MSC++,因此我會提到您需要從Visual Studio的「輸出」選項卡中獲取消息,「錯誤」窗口中會過濾掉註釋,並且對於此目的沒有用處。筆記緊跟在錯誤消息之後。 –
想......想......想......答案其實很簡單。儘管這不是馬虎的問題的藉口,每當詢問編譯問題時,_complete_編譯器輸出都是_mandatory_。 –