dimanche 2 juin 2019

When should I use templated parameters versus construction parameters?

I am implementing a class called FingerprintBuffer that is to store fingerprints. Each fingerprint is essentially just a byte array. The size of each fingerprint and the capacity of the buffer is fixed and already known at compile time. I have two options:

Templated parameters:

template <int FNGPRT_SIZE, int BUFFER_CAP>
class FingerprintBuffer {
 public:
  FingerprintBuffer() {
    buff_ = new char[FNGPRT_SIZE * BUFFER_CAP]();
    size_ = 0;
  }

 private:
  char* buff_;
  int size_;
};

FingerprintBuffer<36, 300> fngprt_buff;

Or constructor parameters:

class FingerprintBuffer {
 public:
  FingerprintBuffer(int fngprt_sz, int buff_cap) {
    fngprt_sz_ = fngprt_sz;
    buff_cap_ = buff_cap
    buff_ = new char[fngprt_sz_ * buff_cap_]();
    size_ = 0;
  }

 private:
  int fngprt_sz_;
  int buff_cap_;
  char* buff_;
  int size_;
};

FingerprintBuffer fngprt_buff(36, 300);

Which option should I go for? And generally speaking under which conditions / assumptions I should choose templated parameters over construction parameters?

Aucun commentaire:

Enregistrer un commentaire