我需要一個資料型別來保存當前資料型別的字串或向量。這是typedef我為此寫的:
typedef std::variant<std::vector<value>, std::string> value;
顯然,這是無效的,value執行此行時未宣告的識別符號也是如此。所以我嘗試首先宣告value為另一種型別,然后在同一型別中使用它:
typedef int value;
typedef std::variant<std::vector<value>, std::string> value;
這也不起作用,因為型別不同。
那么知道了這一點,在 a 中使用當前型別的正確方法是typedef什么?
uj5u.com熱心網友回復:
我想這就是你要問的:
struct Type
{
using Value = std::variant<std::vector<Type>, std::string>;
Value v;
};
uj5u.com熱心網友回復:
取而代之的是,我創建了一個帶有 getter 和 setter 方法的類:
class value {
std::vector<value> children;
std::string val;
public:
int index;
value(
std::variant<std::vector<value>, std::string> v) {
this->set_value(v);
}
void set_value(
std::variant<std::vector<value>, std::string> v) {
if (v.index() == 0) {
children = std::get<std::vector<value>>(v);
index = 0;
} else {
this->val = std::get<std::string>(v);
index = 1;
}
}
std::variant<std::vector<value>, std::string> get_value() {
if (index == 0) {
return children;
} else {
return this->val;
}
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/399176.html
