how can i declare a variable with different types in C++
struct maxKernelBetTwoVec
{
size_t Elem1;
double Elem2;
};
so Elem2 can be int, or double, or string.
how can i declare a variable with different types in C++
struct maxKernelBetTwoVec
{
size_t Elem1;
double Elem2;
};
so Elem2 can be int, or double, or string.
You could use a union, but they are hard to use correctly and safely. A far better option is to use a std::variant
:
struct maxKernelBetTwoVec
{
size_t Elem1;
std::variant<int, double, std::string> Elem2;
};
Unfortunately, std::variant
is only available in C++17. If you can't yet use std::variant
, you can use boost::variant
instead.