我正在嘗試boost-variant
自定義類。我明白,訪問課程內容的安全方式是使用boost::static_visitor
。你知道爲什麼下面的代碼不能編譯?是否有要求使用boost::static_visitor
的簽名/聲明?自定義類的提升變體
我發現這個問題Why can't I visit this custom type with boost::variant?但我沒有得到它。
問候
AFG
#include <iostream>
#include <algorithm>
#include <boost/variant.hpp>
struct CA{};
struct ca_visitor : public boost::static_visitor<CA>
{
const CA& operator()(const CA& obj) const { return obj;}
};
struct CB{};
struct cb_visitor : public boost::static_visitor<CB>
{
const CB& operator()(const CB& obj) const { return obj;}
};
int main(){
typedef boost::variant<
CA
,CB > v_type;
v_type v;
const CA& a = boost::apply_visitor(ca_visitor(), v);
}
類型由式樣的探視可以通過'T * V來實現=獲得(VAR);'其中'v'如果'var'不是'T'類型的'nullptr'。請注意,這是脆弱的。 –
Yakk
2013-03-03 15:35:13
@Yakk:確實,但在我看來,OP在詢問訪問者的正確用法。 – 2013-03-03 15:37:05
當然可以,但我向他們展示瞭如何以他們期望的方式「拜訪」他們的工作方式可能是有用的:很少有人不知道答案知道該問什麼。 – Yakk 2013-03-03 15:47:51