I am designing a Dynamic class that can store any kind of value. It is defined as:
class Dynamic {
private:
Type type_;
std::uint64_t data_;
public:
Dynamic();
...
}
where Type is an enum that contains ids of all the types I can store in the Dynamic object : unsigned char
, signed char
, char
, ..., unsigned short
, short
, std::size_t
, std::ptrdiff_t
, float
, double
, std::vector<unsigned char>
, ..., std::vector<double>
, etc. Think about 200 types.
I would like to construct the Dynamic object very easily. If the type T is supported, the following code should work.
void f(const T& x) {
il::Dynamic d = x;
...
}
It could be implemented very easily with the following constructor:
template <typename T>
Dynamic(const T& x) {
...
}
except that I have the following problem. On 64-bit platforms
const il::Dynamic d0 = 1'000'000'000;
would create an Dynamic containing an int, and
const il::Dynamic d1 = 10'000'000'000;
would create something else (don't even want to know if it is a long, or a long long). I don't want that, and the only integer type I want to use above 32 bits is a signed integer of the same size as std::size_t
which is std::ptrdiff_t
. As a consequence, I want to prevent the constructor to work with the following types : int
, unsigned int
, long
, unsigned long
, long long
, unsigned long long
, except one which is std::ptrdiff_t
. How could I do such as thing?