Access via type

Open
#84 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Start by reviewing the variant get() entry points and how they handle duplicate types, type aliases, std::reference_wrapper, and mapbox::util::recursive_wrapper. Compare that behavior with the issue's discussion of index-based access and determine the intended unambiguous API. Done means the access rules are defined and consistently enforced or supported, with coverage for the ambiguous wrapper cases.

Written by the indexing model from the issue text.

Description

question

Our implementation of variant allows access via get<type>() even if the type appears several times in the variant. This can be potentially confusing and error prone, especially when type aliasing is involved. Say, you got a variant variant<int32_t, int>. Do you know what get<int>() will do? Does the architecture matter?

What makes things even more confusing is the addition of special versions of get that unwrap std::reference_wrapper and mapbox::util::recursive_wrapper on the fly. And they get accessed using the underlying type. So having a variant<recursive_wrapper<int>> you can access that int using the somewhat magical get<int>(). What happens when you access a variant<int, recursive_wrapper<int>> using get<int>()?

I do see the convenience these choices bring, but on the other hand, these could lead to really hard to find bugs. The upcoming standards "solves" the first issue by not allowing get-access through the types if the types are not unique. You have to use index-based access then, which we don't have. It doesn't have the second problem because it doesn't have those magic wrappers.

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

Contributor guide

Open the contributing guide

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.