Skip to content

rust::Slice<T>::iterator missing element_type typedef causes C++20 compilation error on Mac #1574

Description

@jgraettinger

Hi! Thanks so much for your efforts with this library and many others. I developed this bug report with LLM assistance. I've done my best to verify everything here, and have confirmed that adding using element_type = T; does resolve this error, but my C++ is ... Rusty. Generated report follows:

Description

When compiling with C++20 (-std=c++20 or -std=c++2a), code using rust::Slice<const uint8_t> fails to compile with Apple Clang 15 (and likely other compilers). The error occurs because rust::Slice<T>::iterator doesn't provide an element_type typedef that's required by std::pointer_traits for C++20's contiguous iterator concept verification.

Error Message

error: implicit instantiation of undefined template 'std::__pointer_traits_element_type<rust::Slice<const unsigned char>::iterator>'
    typedef typename __pointer_traits_element_type<pointer>::type    element_type;
                     ^

This error is triggered by the static assertion at line 282 of cxx.h:

#if __cplusplus >= 202002L
static_assert(std::ranges::contiguous_range<rust::Slice<const uint8_t>>);

Root Cause Analysis

The C++20 contiguous_iterator concept requires that std::to_address works with the iterator type. The std::to_address implementation checks if std::pointer_traits<Iterator> can be instantiated.

std::pointer_traits attempts to deduce element_type in two ways:

  1. If the type has an explicit element_type member typedef
  2. If the type is a template like SomeTemplate<T, Args...>, it extracts T as the element type

Since rust::Slice<T>::iterator:

  • Doesn't have an element_type typedef (only has value_type)
  • Is a nested class, not a template itself

Neither specialization of __pointer_traits_element_type matches, causing the compilation error.

Minimal Reproduction

#include "rust/cxx.h"

int main() {
    rust::Slice<const uint8_t> slice;
    return 0;
}

Compile with: c++ -std=c++20 test.cpp

Solution

Add using element_type = T; to the rust::Slice<T>::iterator class definition after line 248 in cxx.h:

  using value_type = T;
  using difference_type = std::ptrdiff_t;
  using pointer = typename std::add_pointer<T>::type;
  using reference = typename std::add_lvalue_reference<T>::type;
  using element_type = T;  // <-- Add this line

This allows std::pointer_traits to properly deduce the element type for the iterator.

Environment

  • cxx version: 1.0.168
  • Compiler: Apple clang version 15.0.0 (clang-1500.3.9.4)
  • Platform: macOS (arm64-apple-darwin24.6.0)
  • C++ Standard: C++20

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions