I a trying to make a std::variant that can contain a vector of the same variant:
class ScriptParameter;
using ScriptParameter = std::variant<bool, int, double, std::string, std::vector<ScriptParameter> >;
I am getting ScriptParameter redefinition. It think it is possibly because a template parameter cannot be forward declared?
Is there a way to achieve a variant that could also contain an array of same typed variants?
Since the forward declaration says ScriptParameter
is a class, you can't use using
alias. However, nothing is inherently wrong here, since vector
is only a pointer, there is no real circular dependency.
You can use inheritance:
class ScriptParameter;
class ScriptParameter
: public std::variant<bool, int, double, std::string, std::vector<ScriptParameter> >
{
public:
using base = std::variant<bool, int, double, std::string, std::vector<ScriptParameter> >;
using base::base;
using base::operator=;
};
int main() {
ScriptParameter sp{"hello"};
sp = 1.0;
std::vector<ScriptParameter> vec;
sp = vec;
std::cout << sp.index() << "\n";
}