I would like to make a type that wraps a numeric type (and provides additional functionality).
Furthermore, I need the number and the wrapper to be both implicitly convertible to each other.
So far I have:
template<class T>
struct Wrapper
{
T value;
Wrapper() { }
Wrapper(T const &value) : value(value) { }
// ... operators defined here ...
};
It's almost good, but it doesn't quite behave the same as a built-in type:
#include <iostream>
int main()
{
unsigned int x1, x2 = unsigned int();
Wrapper<unsigned int> y1, y2 = Wrapper<unsigned int>();
std::cerr << x1 << std::endl; // uninitialized, as expected
std::cerr << y1.value << std::endl; // uninitialized, as expected
std::cerr << x2 << std::endl; // zero-initialized, as expected
std::cerr << y2.value << std::endl; // uninitialized!?!
}
Is there any way for me to design the Wrapper
such that statements like
Wrapper<unsigned int> y2 = Wrapper<unsigned int>();
initialize the value
inside, but without forcing statements like
Wrapper<unsigned int> y1;
to also do the same?
In other words, is it possible to make a type that behaves exactly the same as a built-in type in terms of initialization?