mapbox / mapbox/variant

Checks Concepts - Error Messages

Open
#127 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

While working on https://github.com/mapbox/variant/pull/126 I tested what happens if `Ts` are not Hashable.
Turns out the error messages are pretty bad - as expected.

This is true for all features relying on the underlying types. Could e.g. be comparing variants for equality and then variant's `operator==` fails to compile since the underlying types are not comparable.

Here is what we could do: emulate Concepts via Expression SFINAE and use them to provide beautiful error messages. Here is a first example of how this could look on the Hashable side:

```c++
struct hasher
{
template
struct is_hashable : std::false_type {};

template
struct is_hashable{}(std::declval()))> : std::true_type {};

template
std::size_t operator()(const T& hashable) const
{
static_assert(is_hashable::value, "type is not hashable");
return std::hash{}(hashable);
}
};
```

Now error messages are down to

./include/mapbox/variant.hpp:547:9: error: static assertion failed: type is not hashable
static_assert(is_hashable::value, "type is not hashable");

instead of coming from deep down in the stdlib's btree implementation just because you tried to put a variant into an unordered set. Similarly this would work for equality etc.

Note: I'm not up to date on how far Expression SFINAE support is in Microsoft's VS, a couple of years ago is was a no go. Just something to be aware of. On the GCC / Clang side we're good.

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.

Research direction

Start with include/mapbox/variant.hpp around the reported line 547 and review how hashability and other underlying-type operations are currently instantiated. Use the proposed expression-SFINAE approach as the starting point, then consider diagnostics for hashing and equality while checking GCC, Clang, and Microsoft compiler support. Done means unsupported underlying types produce focused error messages without breaking supported C++11/C++14 usage.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.