Skip to content

Commit 3fc625f

Browse files
committed
Avoid warning with rvalue construction without local storage
1 parent 15a2bff commit 3fc625f

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

pxr/base/tf/smallVector.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ template <typename T, uint32_t N>
156156
class TfSmallVector : public TfSmallVectorBase
157157
{
158158
public:
159-
160159
/// XXX: Functionality currently missing, and which we would like to add as
161160
/// needed:
162161
/// - emplace
@@ -240,8 +239,12 @@ class TfSmallVector : public TfSmallVectorBase
240239
// sizes. Note that capacities will be the same in this case, so no
241240
// need to swap those.
242241
else {
243-
_UninitializedMove(rhs.begin(), rhs.end(), begin());
244-
rhs._Destruct();
242+
// If N is 0, this means that rhs is empty and there are no elements that need moving or destroying
243+
// This avoids warnings in GCC.
244+
if constexpr (N > 0) {
245+
_UninitializedMove(rhs.begin(), rhs.end(), begin());
246+
rhs._Destruct();
247+
}
245248
}
246249
std::swap(_size, rhs._size);
247250
}

pxr/base/tf/testenv/smallVector.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,33 @@ testNoLocalStorage()
319319
TF_AXIOM(v.capacity() == 4);
320320
}
321321

322+
static void
323+
testEmptyMoveNoLocalStorage()
324+
{
325+
TfSmallVector<int, 0> empty{};
326+
{
327+
// Empty move construction
328+
TfSmallVector<int, 0> v{std::move(empty)};
329+
TF_AXIOM(v.size() == 0);
330+
TF_AXIOM(v.capacity() == 0);
331+
332+
v.push_back(1414);
333+
TF_AXIOM(v.size() == 1);
334+
TF_AXIOM(v.capacity() == 1);
335+
TF_AXIOM(v.front() == 1414);
336+
TF_AXIOM(v.back() == 1414);
337+
338+
v.push_back(1515);
339+
TF_AXIOM(v.size() == 2);
340+
TF_AXIOM(v.capacity() == 2);
341+
TF_AXIOM(v.front() == 1414);
342+
TF_AXIOM(v.back() == 1515);
343+
}
344+
345+
TF_AXIOM(empty.size() == 0);
346+
TF_AXIOM(empty.capacity() == 0);
347+
}
348+
322349
static void
323350
testGrowth()
324351
{
@@ -1588,6 +1615,8 @@ Test_TfSmallVector()
15881615
testConstructors();
15891616
std::cout << "testNoLocalStorage" << std::endl;
15901617
testNoLocalStorage();
1618+
std::cout << "testEmptyMoveNoLocalStorage" << std::endl;
1619+
testEmptyMoveNoLocalStorage();
15911620
std::cout << "testGrowth" << std::endl;
15921621
testGrowth();
15931622
std::cout << "testIteration" << std::endl;

0 commit comments

Comments
 (0)