Skip to content

Commit ce20c11

Browse files
authored
Return reference from BoundedVector::emplace_back (#730)
Using `auto` made `BoundedVector::emplace_back()` return a copy instead of a reference. Code such as the following would not compile in that case: ``` auto & value = bounded_vector.emplace_back(); ``` Due to the `auto` usage an rvalue was returned which couldn't be bound to an lvalue reference. Signed-off-by: Alexander Hans <[email protected]>
1 parent b93c518 commit ce20c11

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

rosidl_runtime_cpp/include/rosidl_runtime_cpp/bounded_vector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class BoundedVector
474474
* \param args Arguments to be forwarded to the constructor of Tp
475475
*/
476476
template<typename ... Args>
477-
auto
477+
typename Base::reference
478478
emplace_back(Args && ... args)
479479
{
480480
if (size() >= UpperBound) {

rosidl_runtime_cpp/test/test_bounded_vector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ TEST(rosidl_generator_cpp, bounded_vector_rvalue) {
4343
ASSERT_EQ(v.size(), 0u);
4444
ASSERT_EQ(v.max_size(), 2u);
4545
v.emplace_back(1);
46-
v.emplace_back(2);
46+
auto & value = v.emplace_back(2);
47+
ASSERT_EQ(value, 2);
4748
ASSERT_THROW(v.emplace_back(3), std::length_error);
4849
ASSERT_EQ(v.size(), 2u);
4950
// move assignment

0 commit comments

Comments
 (0)