Skip to content

Commit 611ca51

Browse files
[Warnings] Fixed "Possible Dangling Reference Warning"
In GCC-13, a new warning was added to detect dangling references to temporary variables. This is a good addition as these temporaries are deleted the line they are created; but passing a reference to them can cause issues. The NDMatrix class in VTR used const_cast to avoid duplicate code; however, this required a cast to a reference which may be tripping up the warning detection code in the compiler. I do not think this is an actual issue. In truth, the const_casts make the code very hard to read and understand, when all the non-const version is doing is exactly what the const version was doing. Since these functions are so small and simple, I just duplicated the code. I only did this in places where the warning was coming up.
1 parent 49de5fb commit 611ca51

File tree

1 file changed

+9
-51
lines changed

1 file changed

+9
-51
lines changed

libs/libvtrutil/src/vtr_ndmatrix.h

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class NdMatrixProxy {
4141

4242
NdMatrixProxy<T, N>& operator=(const NdMatrixProxy<T, N>& other) = delete;
4343

44-
///@brief const [] operator
45-
const NdMatrixProxy<T, N - 1> operator[](size_t index) const {
44+
///@brief [] operator
45+
NdMatrixProxy<T, N - 1> operator[](size_t index) const {
4646
VTR_ASSERT_SAFE_MSG(index < dim_sizes_[0], "Index out of range (above dimension maximum)");
4747
VTR_ASSERT_SAFE_MSG(dim_sizes_[1] > 0, "Can not index into zero-sized dimension");
4848

@@ -53,12 +53,6 @@ class NdMatrixProxy {
5353
start_ + dim_strides_[0] * index); // Advance to index in this dimension
5454
}
5555

56-
///@brief [] operator
57-
NdMatrixProxy<T, N - 1> operator[](size_t index) {
58-
// Call the const version and cast-away constness
59-
return const_cast<const NdMatrixProxy<T, N>*>(this)->operator[](index);
60-
}
61-
6256
private:
6357
const size_t* dim_sizes_;
6458
const size_t* dim_strides_;
@@ -83,21 +77,15 @@ class NdMatrixProxy<T, 1> {
8377

8478
NdMatrixProxy<T, 1>& operator=(const NdMatrixProxy<T, 1>& other) = delete;
8579

86-
///@brief const [] operator
87-
const T& operator[](size_t index) const {
80+
///@brief [] operator
81+
T& operator[](size_t index) const {
8882
VTR_ASSERT_SAFE_MSG(dim_strides_[0] == 1, "Final dimension must have stride 1");
8983
VTR_ASSERT_SAFE_MSG(index < dim_sizes_[0], "Index out of range (above dimension maximum)");
9084

9185
//Base case
9286
return start_[index];
9387
}
9488

95-
///@brief [] operator
96-
T& operator[](size_t index) {
97-
// Call the const version and cast-away constness
98-
return const_cast<T&>(const_cast<const NdMatrixProxy<T, 1>*>(this)->operator[](index));
99-
}
100-
10189
/**
10290
* @brief Backward compitability
10391
*
@@ -107,16 +95,10 @@ class NdMatrixProxy<T, 1> {
10795
* Note that it is the caller's responsibility to use this correctly; care must be taken
10896
* not to clobber elements in other dimensions
10997
*/
110-
const T* data() const {
98+
T* data() const {
11199
return start_;
112100
}
113101

114-
///@brief same as above but allow update the value
115-
T* data() {
116-
// Call the const version and cast-away constness
117-
return const_cast<T*>(const_cast<const NdMatrixProxy<T, 1>*>(this)->data());
118-
}
119-
120102
private:
121103
const size_t* dim_sizes_;
122104
const size_t* dim_strides_;
@@ -203,14 +185,8 @@ class NdMatrixBase {
203185
return dim_sizes_[i];
204186
}
205187

206-
///@brief const Flat accessors of NdMatrix
207-
const T& get(size_t i) const {
208-
VTR_ASSERT_SAFE(i < size_);
209-
return data_[i];
210-
}
211-
212188
///@brief Flat accessors of NdMatrix
213-
T& get(size_t i) {
189+
T& get(size_t i) const {
214190
VTR_ASSERT_SAFE(i < size_);
215191
return data_[i];
216192
}
@@ -344,13 +320,12 @@ class NdMatrix : public NdMatrixBase<T, N> {
344320
///@brief Use the base constructors
345321
using NdMatrixBase<T, N>::NdMatrixBase;
346322

347-
public:
348323
/**
349324
* @brief Access an element
350325
*
351326
* Returns a proxy-object to allow chained array-style indexing (N >= 2 case)
352327
*/
353-
const NdMatrixProxy<T, N - 1> operator[](size_t index) const {
328+
NdMatrixProxy<T, N - 1> operator[](size_t index) const {
354329
VTR_ASSERT_SAFE_MSG(this->dim_size(0) > 0, "Can not index into size zero dimension");
355330
VTR_ASSERT_SAFE_MSG(this->dim_size(1) > 0, "Can not index into size zero dimension");
356331
VTR_ASSERT_SAFE_MSG(index < this->dim_sizes_[0], "Index out of range (above dimension maximum)");
@@ -361,16 +336,6 @@ class NdMatrix : public NdMatrixBase<T, N> {
361336
this->dim_strides_.data() + 1, //Pass the stride for the next dimension
362337
this->data_.get() + this->dim_strides_[0] * index); //Advance to index in this dimension
363338
}
364-
365-
/**
366-
* @brief Access an element
367-
*
368-
* Returns a proxy-object to allow chained array-style indexing
369-
*/
370-
NdMatrixProxy<T, N - 1> operator[](size_t index) {
371-
//Call the const version, since returned by value don't need to worry about const
372-
return const_cast<const NdMatrix<T, N>*>(this)->operator[](index);
373-
}
374339
};
375340

376341
/**
@@ -384,21 +349,14 @@ class NdMatrix<T, 1> : public NdMatrixBase<T, 1> {
384349
///@brief Use the base constructors
385350
using NdMatrixBase<T, 1>::NdMatrixBase;
386351

387-
public:
388-
///@brief Access an element (immutable)
389-
const T& operator[](size_t index) const {
352+
///@brief Access an element
353+
T& operator[](size_t index) const {
390354
VTR_ASSERT_SAFE_MSG(this->dim_size(0) > 0, "Can not index into size zero dimension");
391355
VTR_ASSERT_SAFE_MSG(index >= 0, "Index out of range (below dimension minimum)");
392356
VTR_ASSERT_SAFE_MSG(index < this->dim_sizes_[0], "Index out of range (above dimension maximum)");
393357

394358
return this->data_[index];
395359
}
396-
397-
///@brief Access an element (mutable)
398-
T& operator[](size_t index) {
399-
//Call the const version, and cast away const-ness
400-
return const_cast<T&>(const_cast<const NdMatrix<T, 1>*>(this)->operator[](index));
401-
}
402360
};
403361

404362
///@brief Convenient short forms for common NdMatricies

0 commit comments

Comments
 (0)