Skip to content

Commit

Permalink
Fix errors and warnings with C++20 in FastTemplate.h
Browse files Browse the repository at this point in the history
The destructor and assignment operators are not allowed to include
template specializations in C++20.

They were not needed anyway, because the class itself is templated.

Also, generalize the assignment operators to take `TH1` and check for
the dimension at runtime. This is necessary to avoid other compiler
warnings, such as:

```
interface/FastTemplate.h:123:34: warning: ‘FastTemplate_t<T>& FastTemplate_t<T>::operator=(const TH1&) [with T = double]’ was hidden [-Woverloaded-virtual=]
  123 |         virtual FastTemplate_t & operator=(const TH1 &other) {
      |                                  ^~~~~~~~
interface/FastTemplate.h:284:24: note:   by ‘FastHisto2D_t<double>::operator=’
  284 |         FastHisto2D_t& operator=(const TH2 &other) {
```

That is a very valid warning! `FastHisto2D_t` inherits from
`FastTemplate_t`, and it's not good if their assignment operators take
different types.
  • Loading branch information
guitargeek committed Nov 28, 2023
1 parent bed1eed commit ca9df6c
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions interface/FastTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ template <typename T> class FastTemplate_t {
}
return *this;
}
virtual FastTemplate_t<T> & operator=(const TH1 &other) {
virtual FastTemplate_t & operator=(const TH1 &other) {
if(other.GetDimension() != 1) {
throw std::invalid_argument("FastTemplate_t assignment error: right hand histogram must be 1-dimensional");
}
if ((int)size() != other.GetNbinsX()) {
size_ = (unsigned int)other.GetNbinsX();
values_.resize(size_);
Expand Down Expand Up @@ -200,7 +203,7 @@ template <typename T, typename U=Double_t> class FastHisto_t : public FastTempla
else this->CopyValues(other);
return *this;
}
~FastHisto_t<T,U>(){}
~FastHisto_t(){}
};
template <typename T, typename U=Double_t> class FastHisto2D_t : public FastTemplate_t<T> {
private:
Expand Down Expand Up @@ -278,9 +281,12 @@ template <typename T, typename U=Double_t> class FastHisto2D_t : public FastTemp
else this->CopyValues(other);
return *this;
}
FastHisto2D_t<T,U>& operator=(const TH2 &other) {
if (GetNbinsX() != other.GetNbinsX() || GetNbinsY() != other.GetNbinsY()) {
FastHisto2D_t<T,U> fh(other);
FastHisto2D_t& operator=(const TH1 &other) {
if(other.GetDimension() != 2) {
throw std::invalid_argument("FastHisto2D_t assignment error: right hand histogram must be 2-dimensional");
}
if (int(GetNbinsX()) != other.GetNbinsX() || int(GetNbinsY()) != other.GetNbinsY()) {
FastHisto2D_t<T,U> fh(static_cast<TH2 const&>(other));
swap(fh);
}
else this->CopyValues(other);
Expand Down Expand Up @@ -374,9 +380,12 @@ template <typename T, typename U=Double_t> class FastHisto3D_t : public FastTemp
else this->CopyValues(other);
return *this;
}
FastHisto3D_t<T,U>& operator=(const TH3 &other) {
if (GetNbinsX() != other.GetNbinsX() || GetNbinsY() != other.GetNbinsY() || GetNbinsZ() != other.GetNbinsZ()) {
FastHisto3D_t<T,U> fh(other);
FastHisto3D_t<T,U>& operator=(const TH1 &other) {
if(other.GetDimension() != 3) {
throw std::invalid_argument("FastHisto3D_t assignment error: right hand histogram must be 3-dimensional");
}
if (int(GetNbinsX()) != other.GetNbinsX() || int(GetNbinsY()) != other.GetNbinsY() || int(GetNbinsZ()) != other.GetNbinsZ()) {
FastHisto3D_t<T,U> fh(static_cast<TH3 const&>(other));
swap(fh);
}
else this->CopyValues(other);
Expand Down

0 comments on commit ca9df6c

Please sign in to comment.