Reputation: 8528
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?
Upvotes: 4
Views: 259
Reputation: 14063
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.
Upvotes: 2