`touches` is poorly specified

Open
#62 2 comments 0 reactions 0 assignees View on GitHub

@jofaber is already working on this.

Since Nov 13, 2026.

  • #67 by @jofaber — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start with include/boost/icl/concept/interval.hpp at the touches implementation linked in the issue, then compare its behavior with the documented function reference and the two reproductions. Decide and implement consistent behavior for reversed argument order and empty intervals, updating documentation and tests to define the expected cases.

Written by the indexing model from the issue text.

Description

The implementation of touches:

https://github.com/boostorg/icl/blob/b587925c05ab626436822a5d5e3317a5da97f320/include/boost/icl/concept/interval.hpp#L1007-L1034

assumes that left is to the left of right, yet this precondition is not documented:

touches(x,y) Between the disjoint intervals x and y are no elements.

So, we end up with touches not being symmetric (https://godbolt.org/z/7zPoe6hYj):

#include <boost/icl/interval.hpp>
#include <iostream>

int main()
{
  boost::icl::closed_interval<int> x(0, 1), y(2, 3);

  std::cout << touches(x, y) << "\n"; // outputs 1
  std::cout << touches(y, x) << "\n"; // outputs 0
}

Additionally, empty intervals are handled in a way that is inconsistent with interval equality (https://godbolt.org/z/dxjGbsYr8):

#include <boost/icl/interval.hpp>
#include <iostream>

int main()
{
  boost::icl::right_open_interval<int> empty0(0, 0), empty1(5, 5), y(0, 1);

  std::cout << (empty0 == empty1) << "\n"; // outputs 1
  std::cout << touches(empty0, y) << "\n"; // outputs 1
  std::cout << touches(empty1, y) << "\n"; // outputs 0
}

In the example empty0 and empty1 are equal, yet one touches y and the other does not, thus breaking the generally assumed equality preservation property.

My suggestions for improvement are:

  • Document the precondition that x must precede y in touches(x, y) or else make touches behave symmetrically (touches(x, y) == touches(y, x) whenever touches(x, y) is well defined).
  • Clarify what "x touches y" means when one or both of x, y are empty, or else explicitly disqualify passing empty intervals to touches.
  • If empty intervals are allowed, make the implementation of touches equality-preserving when an empty interval is replaced by another empty interval.
Dominant language
C++
Stars
15
Forks
51
Avg merge
8d 6h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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/icl

All issues in boostorg/icl

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.