Skip to content

Commit ef1ce29

Browse files
junjieqimeta-codesync[bot]
authored andcommitted
Add explicit Rule of Five defaults for classes with user-provided destructors (#5094)
Summary: Pull Request resolved: #5094 Fix 73 `cppcoreguidelines-special-member-functions` and `clang-diagnostic-deprecated-copy-with-user-provided-dtor` lint warnings across 32 header files by adding explicit `= default` declarations for copy/move constructors and assignment operators to 46 classes that define user-provided destructors. In C++17+, the implicit generation of copy constructors for classes with user-provided destructors is deprecated. Adding explicit `= default` preserves the existing behavior while satisfying the Rule of Five. Default constructors were also added where needed to maintain existing default-constructibility. Differential Revision: D100592061
1 parent 375d57d commit ef1ce29

32 files changed

+324
-15
lines changed

faiss/AutoTune.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ struct AutoTuneCriterion {
5151
virtual double evaluate(const float* D, const idx_t* I) const = 0;
5252

5353
virtual ~AutoTuneCriterion() {}
54+
// rule of five defaults
55+
AutoTuneCriterion(const AutoTuneCriterion&) = default;
56+
AutoTuneCriterion& operator=(const AutoTuneCriterion&) = default;
57+
AutoTuneCriterion(AutoTuneCriterion&&) = default;
58+
AutoTuneCriterion& operator=(AutoTuneCriterion&&) = default;
5459
};
5560

5661
struct OneRecallAtRCriterion : AutoTuneCriterion {
@@ -61,6 +66,11 @@ struct OneRecallAtRCriterion : AutoTuneCriterion {
6166
double evaluate(const float* D, const idx_t* I) const override;
6267

6368
~OneRecallAtRCriterion() override {}
69+
// rule of five defaults
70+
OneRecallAtRCriterion(const OneRecallAtRCriterion&) = default;
71+
OneRecallAtRCriterion& operator=(const OneRecallAtRCriterion&) = default;
72+
OneRecallAtRCriterion(OneRecallAtRCriterion&&) = default;
73+
OneRecallAtRCriterion& operator=(OneRecallAtRCriterion&&) = default;
6474
};
6575

6676
struct IntersectionCriterion : AutoTuneCriterion {
@@ -71,6 +81,11 @@ struct IntersectionCriterion : AutoTuneCriterion {
7181
double evaluate(const float* D, const idx_t* I) const override;
7282

7383
~IntersectionCriterion() override {}
84+
// rule of five defaults
85+
IntersectionCriterion(const IntersectionCriterion&) = default;
86+
IntersectionCriterion& operator=(const IntersectionCriterion&) = default;
87+
IntersectionCriterion(IntersectionCriterion&&) = default;
88+
IntersectionCriterion& operator=(IntersectionCriterion&&) = default;
7489
};
7590

7691
/**
@@ -218,6 +233,11 @@ struct ParameterSpace {
218233
OperatingPoints* ops) const;
219234

220235
virtual ~ParameterSpace() {}
236+
// rule of five defaults
237+
ParameterSpace(const ParameterSpace&) = default;
238+
ParameterSpace& operator=(const ParameterSpace&) = default;
239+
ParameterSpace(ParameterSpace&&) = default;
240+
ParameterSpace& operator=(ParameterSpace&&) = default;
221241
};
222242

223243
} // namespace faiss

faiss/Clustering.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ struct Clustering : ClusteringParameters {
145145
void post_process_centroids();
146146

147147
virtual ~Clustering() {}
148+
// rule of five defaults
149+
Clustering(const Clustering&) = default;
150+
Clustering& operator=(const Clustering&) = default;
151+
Clustering(Clustering&&) = default;
152+
Clustering& operator=(Clustering&&) = default;
148153
};
149154

150155
/** Exact 1D clustering algorithm
@@ -159,6 +164,11 @@ struct Clustering1D : Clustering {
159164
void train_exact(idx_t n, const float* x);
160165

161166
virtual ~Clustering1D() {}
167+
// rule of five defaults
168+
Clustering1D(const Clustering1D&) = default;
169+
Clustering1D& operator=(const Clustering1D&) = default;
170+
Clustering1D(Clustering1D&&) = default;
171+
Clustering1D& operator=(Clustering1D&&) = default;
162172
};
163173

164174
struct ProgressiveDimClusteringParameters : ClusteringParameters {
@@ -174,6 +184,14 @@ struct ProgressiveDimIndexFactory {
174184
virtual Index* operator()(int dim);
175185

176186
virtual ~ProgressiveDimIndexFactory() {}
187+
// rule of five defaults
188+
ProgressiveDimIndexFactory() = default;
189+
ProgressiveDimIndexFactory(const ProgressiveDimIndexFactory&) = default;
190+
ProgressiveDimIndexFactory& operator=(const ProgressiveDimIndexFactory&) =
191+
default;
192+
ProgressiveDimIndexFactory(ProgressiveDimIndexFactory&&) = default;
193+
ProgressiveDimIndexFactory& operator=(ProgressiveDimIndexFactory&&) =
194+
default;
177195
};
178196

179197
/** K-means clustering with progressive dimensions used
@@ -208,6 +226,12 @@ struct ProgressiveDimClustering : ProgressiveDimClusteringParameters {
208226
void train(idx_t n, const float* x, ProgressiveDimIndexFactory& factory);
209227

210228
virtual ~ProgressiveDimClustering() {}
229+
// rule of five defaults
230+
ProgressiveDimClustering(const ProgressiveDimClustering&) = default;
231+
ProgressiveDimClustering& operator=(const ProgressiveDimClustering&) =
232+
default;
233+
ProgressiveDimClustering(ProgressiveDimClustering&&) = default;
234+
ProgressiveDimClustering& operator=(ProgressiveDimClustering&&) = default;
211235
};
212236

213237
/** simplified interface

faiss/Index.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ struct SearchParameters {
9090
IDSelector* sel = nullptr;
9191
/// make sure we can dynamic_cast this
9292
virtual ~SearchParameters() {}
93+
// rule of five defaults
94+
SearchParameters() = default;
95+
SearchParameters(const SearchParameters&) = default;
96+
SearchParameters& operator=(const SearchParameters&) = default;
97+
SearchParameters(SearchParameters&&) = default;
98+
SearchParameters& operator=(SearchParameters&&) = default;
9399
};
94100

95101
/** Abstract structure for an index, supports adding vectors and searching
@@ -123,6 +129,11 @@ struct Index {
123129
metric_arg(0) {}
124130

125131
virtual ~Index();
132+
// rule of five defaults
133+
Index(const Index&) = default;
134+
Index& operator=(const Index&) = default;
135+
Index(Index&&) = default;
136+
Index& operator=(Index&&) = default;
126137

127138
/** Perform training on a representative set of vectors
128139
*

faiss/Index2Layer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ struct Index2Layer : IndexFlatCodes {
4848

4949
Index2Layer();
5050
~Index2Layer() override;
51+
// rule of five defaults
52+
Index2Layer(const Index2Layer&) = default;
53+
Index2Layer& operator=(const Index2Layer&) = default;
54+
Index2Layer(Index2Layer&&) = default;
55+
Index2Layer& operator=(Index2Layer&&) = default;
5156

5257
void train(idx_t n, const float* x) override;
5358

@@ -71,6 +76,7 @@ struct Index2Layer : IndexFlatCodes {
7176
};
7277

7378
// block size used in Index2Layer::sa_encode
74-
FAISS_API extern int index2layer_sa_encode_bs;
79+
FAISS_API extern int
80+
index2layer_sa_encode_bs; // NOLINT(facebook-avoid-non-const-global-variables)
7581

7682
} // namespace faiss

faiss/IndexAdditiveQuantizer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ struct AdditiveCoarseQuantizer : Index {
191191
struct SearchParametersResidualCoarseQuantizer : SearchParameters {
192192
float beam_factor = 4.0f;
193193
~SearchParametersResidualCoarseQuantizer() {}
194+
// rule of five defaults
195+
SearchParametersResidualCoarseQuantizer(
196+
const SearchParametersResidualCoarseQuantizer&) = default;
197+
SearchParametersResidualCoarseQuantizer& operator=(
198+
const SearchParametersResidualCoarseQuantizer&) = default;
199+
SearchParametersResidualCoarseQuantizer(
200+
SearchParametersResidualCoarseQuantizer&&) = default;
201+
SearchParametersResidualCoarseQuantizer& operator=(
202+
SearchParametersResidualCoarseQuantizer&&) = default;
194203
};
195204

196205
/** The ResidualCoarseQuantizer is a bit specialized compared to the

faiss/IndexAdditiveQuantizerFastScan.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ struct IndexAdditiveQuantizerFastScan : IndexFastScan {
5050
IndexAdditiveQuantizerFastScan();
5151

5252
~IndexAdditiveQuantizerFastScan() override;
53+
// rule of five defaults
54+
IndexAdditiveQuantizerFastScan(const IndexAdditiveQuantizerFastScan&) =
55+
default;
56+
IndexAdditiveQuantizerFastScan& operator=(
57+
const IndexAdditiveQuantizerFastScan&) = default;
58+
IndexAdditiveQuantizerFastScan(IndexAdditiveQuantizerFastScan&&) = default;
59+
IndexAdditiveQuantizerFastScan& operator=(
60+
IndexAdditiveQuantizerFastScan&&) = default;
5361

5462
/// build from an existing IndexAQ
5563
explicit IndexAdditiveQuantizerFastScan(

faiss/IndexBinary.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ struct IndexBinary {
4545
explicit IndexBinary(idx_t d = 0, MetricType metric = METRIC_L2);
4646

4747
virtual ~IndexBinary();
48+
// rule of five defaults
49+
IndexBinary(const IndexBinary&) = default;
50+
IndexBinary& operator=(const IndexBinary&) = default;
51+
IndexBinary(IndexBinary&&) = default;
52+
IndexBinary& operator=(IndexBinary&&) = default;
4853

4954
/** Perform training on a representative set of vectors.
5055
*

faiss/IndexBinaryFromFloat.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ struct IndexBinaryFromFloat : IndexBinary {
3333
explicit IndexBinaryFromFloat(Index* index);
3434

3535
~IndexBinaryFromFloat() override;
36+
// rule of five defaults
37+
IndexBinaryFromFloat(const IndexBinaryFromFloat&) = default;
38+
IndexBinaryFromFloat& operator=(const IndexBinaryFromFloat&) = default;
39+
IndexBinaryFromFloat(IndexBinaryFromFloat&&) = default;
40+
IndexBinaryFromFloat& operator=(IndexBinaryFromFloat&&) = default;
3641

3742
void add(idx_t n, const uint8_t* x) override;
3843

faiss/IndexBinaryHNSW.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ struct IndexBinaryHNSW : IndexBinary {
4545
explicit IndexBinaryHNSW(IndexBinary* storage, int M = 32);
4646

4747
~IndexBinaryHNSW() override;
48+
// rule of five defaults
49+
IndexBinaryHNSW(const IndexBinaryHNSW&) = default;
50+
IndexBinaryHNSW& operator=(const IndexBinaryHNSW&) = default;
51+
IndexBinaryHNSW(IndexBinaryHNSW&&) = default;
52+
IndexBinaryHNSW& operator=(IndexBinaryHNSW&&) = default;
4853

4954
DistanceComputer* get_distance_computer() const;
5055

faiss/IndexBinaryHash.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ struct IndexBinaryHashStats {
7777
void reset();
7878
};
7979

80-
FAISS_API extern IndexBinaryHashStats indexBinaryHash_stats;
80+
FAISS_API extern IndexBinaryHashStats
81+
indexBinaryHash_stats; // NOLINT(facebook-avoid-non-const-global-variables)
8182

8283
/** just uses the b first bits as a hash value */
8384
struct IndexBinaryMultiHash : IndexBinary {
@@ -100,6 +101,11 @@ struct IndexBinaryMultiHash : IndexBinary {
100101
IndexBinaryMultiHash();
101102

102103
~IndexBinaryMultiHash() override;
104+
// rule of five defaults
105+
IndexBinaryMultiHash(const IndexBinaryMultiHash&) = default;
106+
IndexBinaryMultiHash& operator=(const IndexBinaryMultiHash&) = default;
107+
IndexBinaryMultiHash(IndexBinaryMultiHash&&) = default;
108+
IndexBinaryMultiHash& operator=(IndexBinaryMultiHash&&) = default;
103109

104110
void reset() override;
105111

0 commit comments

Comments
 (0)