Skip to content

Commit 07d074a

Browse files
authored
tidied duplicate const and non-const member functions (#64)
* tidied duplicate const and non-const member functions * code format
1 parent 8caecef commit 07d074a

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

L1Trigger/TrackFindingTMTT/interface/Array2D.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef L1Trigger_TrackFindingTMTT_Array2D_h
22
#define L1Trigger_TrackFindingTMTT_Array2D_h
33

4+
#include "FWCore/Utilities/interface/Exception.h"
5+
46
#include <vector>
5-
#include <stdexcept>
7+
#include <utility>
68

79
//=== Generic 2D array class.
810

@@ -20,19 +22,16 @@ namespace tmtt {
2022
Array2D(unsigned int m, unsigned int n) : array2D_(m * n), m_{m}, n_{n} {}
2123

2224
const T& operator()(unsigned int i, unsigned int j) const {
23-
checkBounds(i, j);
24-
return array2D_.at(i * n_ + j);
25-
}
26-
27-
T& operator()(unsigned int i, unsigned int j) {
28-
checkBounds(i, j);
25+
if (i >= m_ || j >= n_)
26+
throw cms::Exception("LogicError")
27+
<< "Array2D: indices out of range " << i << " " << j << " " << m_ << " " << n_;
2928
return array2D_[i * n_ + j];
3029
}
3130

32-
private:
33-
void checkBounds(unsigned int i, unsigned int j) const {
34-
if (i >= m_ || j >= n_)
35-
throw std::out_of_range("matrix access out of bounds");
31+
T& operator()(unsigned int i, unsigned int j) {
32+
// Non-const version of operator, without needing to duplicate code.
33+
// (Scott Meyers trick).
34+
return const_cast<T&>(std::as_const(*this)(i, j));
3635
}
3736

3837
private:

L1Trigger/TrackFindingTMTT/src/StubFEWindows.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
55

66
#include <algorithm>
7+
#include <utility>
78

89
using namespace std;
910

@@ -70,9 +71,8 @@ namespace tmtt {
7071

7172
double* StubFEWindows::storedWindowSize(const TrackerTopology* trackerTopo, const DetId& detId) {
7273
// Code accessing geometry inspired by L1Trigger/TrackTrigger/src/TTStubAlgorithm_official.cc
73-
74-
// Scott Meyers's solution to give const & non-const versions of same function, without
75-
// code duplication.
76-
return const_cast<double*>((static_cast<const StubFEWindows*>(this))->storedWindowSize(trackerTopo, detId));
74+
// Non-const version of operator, without needing to duplicate code.
75+
// (Scott Meyers trick).
76+
return const_cast<double*>(std::as_const(*this).storedWindowSize(trackerTopo, detId));
7777
}
7878
} // namespace tmtt

0 commit comments

Comments
 (0)