Consider implementing the <=> for cases unsupported by std::variant

Open
#24 0 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
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Start by reviewing variant2's existing comparison support and the corresponding std::variant behavior. Clarify whether empty alternatives should be treated as strongly equal and how unsupported comparisons should behave, then define tests covering empty structs and mixed alternatives. Done means the intended comparison rules are agreed and verified by tests.

Written by the indexing model from the issue text.

Description

First of all IDK if there are any spaceship plans for variant2 so this is highly speculative.

Secondly I am not some PL expert, and I do not know all C++ corner cases that might make this impossible, this is purely based on what I consider nicer to use.

In the following example to get std::variant <=> to work we must provide<=>for std::is_empty_v<T> == true kind of struct although it has no state.

#include<type_traits>
#include<variant>

struct Empty{
 // variant <=> does not compile without this line       
 auto operator<=>(const Empty& other) const = default;
};

static_assert(std::is_empty_v<Empty>);

int main() {
    std::variant<Empty,int,float> v1,v2;
    v1<=>v2;
}

If <=> support is added to the variant2 it would be nice if he would be able to skip over empty structs when checking that all elements have implemented <=> (he would treat all instances of empty structs as being std::strong_order::equal).

Convenience motivation for this is that unlike some other languages C++ enums can not carry state( IIRC Rust, Swift... can) so people sometimes use variant of structs as a C++ enum. Now some of those structs are pure "tag" or enum struct without any state, it is a bit of a shame to be required add a <=> to them just so they can be put in variant2.

Dominant language
C++
Stars
71
Forks
41
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

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 boostorg/variant2

All issues in boostorg/variant2

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.