Skip to content

fix(queue_adapter, stack_adapter): return emplaced element reference from emplace - #250

Open
reginaldalfret wants to merge 1 commit into
teslamotors:mainfrom
reginaldalfret:fix/tesla-fixed-containers-queue-stack-adapter-emplace-return
Open

reginaldalfret wants to merge 1 commit into
teslamotors:mainfrom
reginaldalfret:fix/tesla-fixed-containers-queue-stack-adapter-emplace-return

Conversation

@reginaldalfret

Copy link
Copy Markdown

Summary

In queue_adapter.hpp and stack_adapter.hpp, emplace declares a constexpr decltype(auto) return type:

template <class... Args>
constexpr decltype(auto) emplace(Args&&... args)
{
    IMPLEMENTATION_DETAIL_DO_NOT_USE_data_.emplace_back(std::forward<Args>(args)...);
}

However, the body was missing a return keyword before invoking IMPLEMENTATION_DETAIL_DO_NOT_USE_data_.emplace_back(...).

Because the underlying container's emplace_back(...) returns a reference to the emplaced element (e.g. FixedVector::emplace_back and FixedCircularBuffer::emplace_back), omitting the return statement in a non-void function leads to undefined behavior if the caller attempts to use the return value, and deviates from standard C++ container adapter behavior (std::queue::emplace and std::stack::emplace in C++17/C++23 return decltype(auto) / reference).

Changes

  • Added return to queue_adapter::emplace.
  • Added return to stack_adapter::emplace.
  • Added regression tests in test/fixed_queue_test.cpp, test/fixed_stack_test.cpp, and test/fixed_circular_queue_test.cpp verifying the returned reference points to the newly constructed element and supports mutations in-place.
  • Verified compilation and runtime correctness across all three container types under Clang C++20.

…from emplace

Signed-off-by: Reginald Alfret <reginaldalfret@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant