Friday, December 2, 2016

get boost variant value using template specialization on literal value

It seems C++17 will have get<0> and get<N> but boost does not have that yet.
boost only have get<type> and which=0,1,2,3,4,5
The following specialization can hack get<N> for now

#pragma once
#include <boost\variant\get.hpp>
#include "JSONWrapper.hpp"
#include <string>

template<int N>
auto get2(JSONValue jsv);

template<>
auto get2<0>(JSONValue jsv)
{
 return boost::get<bool>(jsv);
}

template<>
auto get2<1>(JSONValue jsv)
{
 return boost::get<double>(jsv);
}

template<>
auto get2<2>(JSONValue jsv)
{
 return boost::get<std::string>(jsv);
}

template<>
auto get2<3>(JSONValue jsv)
{
 return boost::get<nullptr_t>(jsv);
}

template<>
auto get2<4>(JSONValue jsv)
{
 return boost::get<JSONArray>(jsv);
}

template<>
auto get2<5>(JSONValue jsv)
{
 return boost::get<JSONObject>(jsv);
}

No comments:

Post a Comment