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:
- If the type has an explicit
element_type member typedef
- 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
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++20or-std=c++2a), code usingrust::Slice<const uint8_t>fails to compile with Apple Clang 15 (and likely other compilers). The error occurs becauserust::Slice<T>::iteratordoesn't provide anelement_typetypedef that's required bystd::pointer_traitsfor C++20's contiguous iterator concept verification.Error Message
This error is triggered by the static assertion at line 282 of cxx.h:
Root Cause Analysis
The C++20
contiguous_iteratorconcept requires thatstd::to_addressworks with the iterator type. Thestd::to_addressimplementation checks ifstd::pointer_traits<Iterator>can be instantiated.std::pointer_traitsattempts to deduceelement_typein two ways:element_typemember typedefSomeTemplate<T, Args...>, it extractsTas the element typeSince
rust::Slice<T>::iterator:element_typetypedef (only hasvalue_type)Neither specialization of
__pointer_traits_element_typematches, causing the compilation error.Minimal Reproduction
Compile with:
c++ -std=c++20 test.cppSolution
Add
using element_type = T;to therust::Slice<T>::iteratorclass definition after line 248 in cxx.h:This allows
std::pointer_traitsto properly deduce the element type for the iterator.Environment