vendredi 24 mai 2019

Does the default constructor represent zero for generic types in C++

Assume there is a function and it works

template <typename T> 
Range<T> IRange(T lower, T upper){
  ....
}

So IRange(0,5)

will produce a Range with values [0,1,2,3,4]. The exact behaviour of Range class is not important.

However now I want a helper function

template <typename T> 
Range<T>
Repeat(T t){
    return IRange(T(), t);
}

The question is: Is using T() to get the zero value generically an ok thing to do. I think so but something nags at me that this might be wrong.

Mostly this class will be used with integer types but anything else that fits the concept should work as well.

Aucun commentaire:

Enregistrer un commentaire