I have in mind a construct like this:
template <typename T, T defaultValue>
struct Numeric
{
Numeric(T t=defaultValue) : value(t) { }
T value;
T operator=()(T t);
operator T();
};
I might use it like this:
std::vector<Numeric<bool, true> > nothingButTheTruth;
My question is simple: Is this a good approach and if so, does something like this exist in a standard library or Boost?
The pattern I see more commonly is to parameterize the container, not the type.
There are a lot of downsides to doing it your way:
bool&
to a Numeric<bool, true>
. vector<bool>
and a vector<Numeric<bool, true> >
are unrelated types. This gets pretty painful pretty quickly. I wouldn't do it, but perhaps you have a strong use case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.