I have a Variant type which I want to use in something like a JSON parser. A type in JSON can include objects and arrays. These objects and arrays can contain members of their own type:
typedef variant<int,float,bool,std::string> baseType;
typedef std::vector<baseType> arrayType; // problem, can't have arrays of arrays
typedef std::unordered_map<std::string,baseType> objType; // problem, can't have objects or arrays within objects.
How do I create a "recursive" templated type?
Something like:
typedef variant<int,float,bool,std::string,std::vector<type>,std::unordered_map<std::string,type> type;
I'm aware of boost; I'm not interested in answers that use it. I've tried to work out how it does it with recursive_wrapper
but the liberal use of pre-processor macros is making it very hard for me.
EDIT
With the help of Yakk below, I've gotten this far:
template<typename... Ts>
class Variant;
template<class T>
struct tag
{
using type=T;
};
template<class X, class A, class B>
struct subst : tag<X>
{};
template<class X, class A, class B>
using subst_t = typename subst<X,A,B>::type;
template<class A, class B>
struct subst<A,A,B> : tag<B>
{};
template<class X, class A, class B>
struct subst<X&,A,B> : tag<subst_t<X,A,B>&>
{};
template<class X, class A, class B>
struct subst<X&&,A,B> : tag<subst_t<X,A,B>&&>
{};
template<class X, class A, class B>
struct subst<X const,A,B> : tag<subst_t<X,A,B>const>
{};
template<class X, class A, class B>
struct subst<X volatile,A,B> : tag<subst_t<X,A,B>volatile>
{};
template<class X, class A, class B>
struct subst<X const volatile,A,B> : tag<subst_t<X,A,B>const volatile>
{};
template<template<class...> class Z, class... Xs, class A, class B>
struct subst<Z<Xs...>,A,B> : tag<Z<subst_t<Xs,A,B>...>>
{};
template<template<class,size_t> class Z, class X, size_t n, class A, class B>
struct subst<Z<X,n>,A,B> : tag<Z<subst_t<X,A,B>,n>>
{};
template<class R, class...Xs, class A, class B>
struct subst<R(Xs...),A,B> : tag<subst_t<R,A,B>(subst_t<Xs,A,B>...)>
{};
struct RecursiveType {};
template<typename Sig>
struct RecursiveVariant
{
using VariantType = Variant<subst_t<Sig,RecursiveType,RecursiveVariant>>;
template<typename V,
typename std::enable_if<
!std::is_same<RecursiveVariant,typename std::decay<V>::type>::value
&& std::is_convertible<V,VariantType>::value
>
::type>
RecursiveVariant(V&& vIn)
:
m_variant(vIn)
{}
RecursiveVariant(){};
template<typename T, typename... Args>
void Set(Args&&... args)
{
m_variant.Set<T,Args...>(std::forward<Args>(args)...);
}
template<typename T>
const T& Get() const
{
return m_variant.Get<T>();
}
template<typename T>
T& Get()
{
return m_variant.Get<T>();
}
VariantType m_variant;
};
The actual Variant type I have is here https://codereview.stackexchange.com/questions/127372/variant-class-that-i-dont-think-is-missing-anything
I think the above RecursiveWrapper
is way off. As I understand it, I should use it as the actual type, i.e.
RecursiveWrapper<RecursiveType> Variant
However, this can't be correct because nowhere have I specified the types allowed in Variant
. Questions about the code I don't understand are
- What is
RecursiveType
? - How can I forward variadic template types to the underlying
Variant
?