C ++ non-zero default value for numeric types - rethinking?

I mean 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 could 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 the standard library or Boost?

+4
source share
1 answer

The pattern I see more often is to parameterize the container, not the type.

There are many drawbacks to doing this in your own way:

  • As long as you provide assignment and conversion, you cannot actually bind a bool& to Numeric<bool, true> .
  • A vector<bool> and a vector<Numeric<bool, true> > are not related types.

It gets pretty painful pretty quickly. I would not do this, but perhaps you have a strong precedent.

+2
source

Source: https://habr.com/ru/post/1393466/


All Articles