Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Implement swap.

Open
#80 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Start by reading the existing variant move and swap-related code, then run the tests in test/t/variant.cpp around lines 268-300. Compare the current behavior with the issue's same-index and different-index swap requirements. Done means the tests pass and swap can use the contained type's more efficient operation when appropriate.

Written by the indexing model from the issue text.

Description

enhancement

In the current code there is no implementation for swap() which means std::swap() will be used. This should do the right thing (as long as the move constructor and move assignment do the right thing) and the tests show this.

But this solution is not optimal, because the default std::swap() will always do one move construction and two move assignments (in this case of the variant). If there is a more efficient swap for the type in the variant, it will not be used. The upcoming standard P0088R0 describes the effects of swap as: "if index() == rhs.index(), calls swap(get<i>(*this), get<i>(hrs)) with i being index(). Else calls swap(*this, hrs)."

We might want to implement our own swap in the future.

Dominant language
C++
Stars
385
Forks
96
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mapbox/variant

All issues in mapbox/variant

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.