I'm trying to write a template class for std::variant<>
that could hold and manage different std::variant<>
containers, for example one template should manage std:variant<int, double>
and the other std::variant<int, double, std::string, std::vector<int>>
.
The main problem might be class member iVariant
defined as a typename
.
My code looks like this so far.
#include <variant>
#include <vector>
#include <string>
#include <iostream>
typedef std::vector<uint8_t> byte_array_t;
typedef std::variant <std::monostate, int, unsigned int, int64_t, uint64_t, std::string, bool, double, byte_array_t> variant_long;
typedef std::variant <std::monostate, int, unsigned int, int64_t, uint64_t, double> variant_short;
template <typename VariantType = variant_long>
class VariantHolder
{
private:
VariantType iVariant;
public:
VariantHolder()
{
}
~VariantHolder()
{
}
template< typename T>
void EmplaceValue( T aValue )
{
iVariant.emplace<T>( aValue );
}
/* Invoke HoldsValue()<T> first to make sure that VariantHolder is capable of returning T-type value */
template< typename T>
T GetValue()
{
return std::get<T>( iVariant );
}
template< typename T>
bool HoldsValue()
{
return std::holds_alternative<T>( iVariant ) ? true : false;
}
bool IsEmpty()
{
return std::holds_alternative<std::monostate>( iVariant ) ? true : false ;
}
void Clear()
{
EmplaceValue( std::monostate{} );
}
};
int main()
{
VariantHolder<variant_long> lVariant;
lVariant.EmplaceValue<int>(64);
if( lVariant.HoldsValue<int>() )
{
auto lIntVal = lVariant.GetValue<int>();
std::cout << lIntVal;
}
// VariantHolder<variant_long> lVariant;
// lVariant.EmplaceValue("std-string");
// if( lVariant.HoldsValue<std::string>() )
// {
// auto lIntVal = lVariant.GetValue<std::string>();
// std::cout << lIntVal;
// }
return 0;
}
I'm getting an error in function
template< typename T>
void EmplaceValue( T aValue )
{
iVariant.emplace<T>( aValue );
}
Which says
expected primary-expression before ‘>’ token iVariant.emplace( aValue );
Is is actually possible to achieve such a template class in this case?