Skip to content

Fix empty vector argument array construction - #218

Merged
zayenz merged 1 commit into
mainfrom
feature/empty-vector-args
Jul 11, 2026
Merged

Fix empty vector argument array construction#218
zayenz merged 1 commit into
mainfrom
feature/empty-vector-args

Conversation

@zayenz

@zayenz zayenz commented Jul 7, 2026

Copy link
Copy Markdown
Member

Summary

  • Fix construction of argument arrays from empty std::vector instances by avoiding &aa[0] when the vector is empty.
  • Add a regression test for empty vector construction of IntArgs, IntVarArgs, and BoolVarArgs.
  • Add a changelog entry thanking @intractabilis.

Fixes #205.

Validation

  • cmake -S . -B /tmp/gecode-empty-vector-args-build -DCMAKE_BUILD_TYPE=Debug -DGECODE_ENABLE_EXAMPLES=OFF -DGECODE_ENABLE_GIST=OFF -DGECODE_ENABLE_QT=OFF
  • cmake --build /tmp/gecode-empty-vector-args-build --target gecode-test -j 8
  • /tmp/gecode-empty-vector-args-build/bin/gecode-test -test Array::Vector::Empty
  • git diff --check

Comment on lines +1560 to +1561
if (n > 0)
heap.copy<T>(a,aa.data(),n);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: aa.data() is fine to call on an empty std::vector (might or might not return nullptr). do we need the if (n > 0) then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, and thanks for noting that. I still decided to keep the redundant if, as it shouldn't cost anything at runtime, and I like that it is explicit and not relying on API promises.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The canonical way is

if (!aa.empty())
    heap.copy<T>(a,aa.data(),n);

@zayenz
zayenz force-pushed the feature/empty-vector-args branch 3 times, most recently from 4b0a8c4 to ba29920 Compare July 11, 2026 05:22
@zayenz
zayenz marked this pull request as ready for review July 11, 2026 06:13
@zayenz
zayenz force-pushed the feature/empty-vector-args branch from ba29920 to 8dd3af4 Compare July 11, 2026 08:23
@zayenz
zayenz merged commit e767f4c into main Jul 11, 2026
14 checks passed
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.

3 participants