Unifying std::span and std::string_view interfaces
Theme
Contents

Unifying std::span and std::string_view interfaces

Unifying std::span and std::string_view interfaces

Document #: P3837R0 Latest Status
Date: 2026-11-25
Project: Programming Language C++
Audience: Library Evolution Working Group
Library Working Group
Reply-to: Mateusz Pusz
<>

1 Introduction

This paper proposes to unify the interfaces of std::span and std::string_view by adding missing member functions to both types. The proposed changes:

  1. Add first and last member functions to std::string_view
  2. Add remove_prefix and remove_suffix member functions to dynamic-sized std::span

2 Motivation

std::span and std::string_view are both lightweight, non-owning views over contiguous sequences in the C++ Standard Library. They serve similar purposes but have inconsistent interfaces that complicate generic programming and increase cognitive load.

Consider the following operations:

std::string_view sv = "Hello, World!";
std::span sp{sv.data(), sv.size()};    // dynamic-sized span

// Getting first N elements
auto first5_sv = sv.substr(0, 5);      // "Hello"
auto first5_sp = sp.first(5);          // equivalent span

// Getting last N elements  
auto last6_sv = sv.substr(sv.size() - 6);  // "World!"
auto last6_sp = sp.last(6);                // equivalent span

// Removing prefix
sv.remove_prefix(7);                   // "World!"
// sp.remove_prefix(7);                // ERROR: no such member function

// Removing suffix
sv.remove_suffix(1);                   // "World"
// sp.remove_suffix(1);                // ERROR: no such member function

This inconsistency forces developers to:

  1. Remember different APIs for similar operations
  2. Write non-generic code that handles each type separately
  3. Use verbose workarounds for missing functionality

Here’s an example of generic code that currently requires specialization:

template<typename View>
void process_middle_part(View& view) {
    if constexpr (std::is_same_v<View, std::string_view>) {
        view.remove_prefix(1);
        view.remove_suffix(1);
    } else if constexpr (requires { view.first(1); }) {
        // For span, we need to reassign since remove_* don't exist
        auto size = view.size();
        view = view.subspan(1, size - 2);
    }
}

With unified interfaces, this becomes simply:

template<typename View>
void process_middle_part(View& view) {
    view.remove_prefix(1);
    view.remove_suffix(1);
}

Note: The above simplified example assumes the view is dynamically-sized (std::string_view
or std::span<T, std::dynamic_extent>). For fixed-size spans, remove_prefix and remove_suffix
are not available, requiring conditional compilation or concepts to handle both cases.

3 Problem statement

The current state of std::span and std::string_view interfaces creates several issues:

3.1 Interface Inconsistency

Operation
std::string_view
std::span
Generic Code Impact
Get first N elements substr(0, n) first(n) Requires different APIs
Get last N elements substr(size()-n) last(n) Requires different APIs
Remove prefix remove_prefix(n) ❌ Missing Forces workarounds
Remove suffix remove_suffix(n) ❌ Missing Forces workarounds

3.2 Generic Programming Difficulties

Writing generic algorithms that work with both types requires template specialization or SFINAE techniques, increasing complexity and reducing maintainability.

3.3 Teachability Issues

New C++ developers must learn different interfaces for conceptually similar operations, violating the principle of least surprise.

4 Proposed Solution

We propose to add the missing member functions to both types, making their interfaces consistent and complete.

4.1 Core Proposal

4.1.1 Add to std::string_view

4.1.2 Add to std::span

Note: The remove_prefix and remove_suffix functions are only added to span<T, dynamic_extent> because fixed-size spans cannot change their extent. This preserves semantic consistency with string_view behavior (in-place modification) while avoiding the confusion that would arise from having the same function names perform different operations (modification vs. returning new views).

5 Impact on existing code

The proposed solution is fully backward compatible:

Example of enhanced generic code:

// Before: Complex, type-specific code
template<typename View>
auto get_middle(const View& view) {
    if constexpr (std::is_same_v<View, std::string_view>) {
        return view.substr(1, view.size() - 2);
    } else {
        return view.subspan(1, view.size() - 2);
    }
}

// After: Simple, unified interface for dynamic spans and string_view
template<typename View>
void trim_ends(View& view) {
    if constexpr (requires { view.remove_prefix(1); }) {
        // Works for string_view and dynamic span
        view.remove_prefix(1);
        view.remove_suffix(1);
    } else {
        // Fallback for fixed span
        view = view.subspan(1, view.size() - 2);
    }
}

// For cases where you know you have a dynamic span or string_view:
template<typename DynamicView>
  requires requires(DynamicView& v) { v.remove_prefix(1); v.remove_suffix(1); }
void trim_ends_simple(DynamicView& view) {
    view.remove_prefix(1);  // Works for both!
    view.remove_suffix(1);  // Works for both!
}

6 Design Alternatives and Rationale

The most critical design decision in this proposal is how to handle remove_prefix and remove_suffix for fixed-size spans (span<T, N> where N != dynamic_extent). Since fixed-size spans cannot change their extent, these functions cannot perform in-place modification as they do for string_view.

We thoroughly analyzed three potential approaches:

6.1 Alternative 1: Return New Views (Like first/last)

6.1.1 Implementation Approach

// Hypothetical approach - NOT proposed
template<size_t Extent>
constexpr auto remove_prefix(size_type n) const -> span<element_type, Extent == dynamic_extent ? dynamic_extent : (Extent - n)>;

template<size_t Extent>  
constexpr auto remove_suffix(size_type n) const -> span<element_type, Extent == dynamic_extent ? dynamic_extent : (Extent - n)>;

6.1.2 Advantages

6.1.3 Disadvantages

6.1.4 Example of the Problem

template<typename View>
void process_data(View& view) {
    auto original_size = view.size();
    view.remove_prefix(10);  // What does this do?
    
    // For string_view: view is modified, size() != original_size
    // For span: view unchanged, size() == original_size
    // This semantic difference could cause silent bugs
}

6.2 Alternative 2: Hybrid Approach with Different Function Names

6.2.1 Approach for Alternative 2

// For dynamic spans only
constexpr void remove_prefix(size_type n);     // In-place modification
constexpr void remove_suffix(size_type n);     // In-place modification

// For all spans
constexpr auto trim_prefix(size_type n) const; // Returns new view
constexpr auto trim_suffix(size_type n) const; // Returns new view

6.2.2 Benefits of Alternative 2

6.2.3 Drawbacks of Alternative 2

6.2.4 Example Complexity

template<typename View>
void trim_edges(View& view) {
    if constexpr (requires { view.remove_prefix(1); }) {
        view.remove_prefix(1);  // Modify in-place
        view.remove_suffix(1);
    } else {
        view = view.trim_prefix(1);  // Different function name!
        view = view.trim_suffix(1);
    }
}

6.3 Alternative 2.5: Add trim_prefix/trim_suffix to string_view

6.3.1 Approach for Alternative 2.5

// Add to std::string_view
constexpr string_view trim_prefix(size_type n) const;  // Returns new view
constexpr string_view trim_suffix(size_type n) const;  // Returns new view

// For all spans (existing functionality)
constexpr auto trim_prefix(size_type n) const;  // Returns new view  
constexpr auto trim_suffix(size_type n) const;  // Returns new view

This would maintain complete interface consistency by providing view-returning functions
across both types while keeping the in-place modification functions
(remove_prefix/remove_suffix) available where semantically appropriate.

6.3.2 Benefits of Alternative 2.5

6.3.3 Drawbacks of Alternative 2.5

6.3.4 Example Usage

template<typename View>
auto get_middle_part(const View& view) {
    // Works universally - always returns a new view
    return view.trim_prefix(1).trim_suffix(1);
}

Note: While this alternative would provide complete interface symmetry, the trim_* functions
can always be added in a future proposal if deemed valuable after gaining experience with the
core unified interface.

6.4 Alternative 3: Only Provide for Dynamic Spans (Chosen)

6.4.1 Approach for Alternative 3

// Only available for span<T, dynamic_extent>
constexpr void remove_prefix(size_type n);     // Modifies in-place
constexpr void remove_suffix(size_type n);     // Modifies in-place

6.4.2 Benefits of Alternative 3

6.4.3 Drawbacks of Alternative 3

6.4.4 Generic Programming Solution

template<typename View>
void trim_edges(View& view) {
    if constexpr (requires { view.remove_prefix(1); }) {
        // Works for string_view and dynamic span
        view.remove_prefix(1);
        view.remove_suffix(1);
    } else {
        // Clear fallback for fixed span
        view = view.subspan(1, view.size() - 2);
    }
}

6.5 Decision Rationale

We chose Alternative 3 (only dynamic spans) for the following reasons:

6.5.1 1. Semantic Consistency is Paramount

The most important principle is that identical function names should have identical semantics across the standard library. Having remove_prefix mean “modify in-place” for string_view but “return new view” for span would violate this fundamental design principle.

6.5.2 2. Clear Compilation Errors Over Silent Differences

It’s better to get a clear compilation error when trying to use remove_prefix on a fixed span than to have the same code work but behave differently for different types.

6.5.3 3. Follows Established Patterns

The standard library already has extent-specific operations for the same class template:

This follows the principle that member function availability should match the type’s semantic
capabilities.

6.5.4 4. Enables Clean Generic Programming

Modern C++ provides excellent tools (requires expressions, concepts) for handling type-specific capabilities in generic code, making this a manageable constraint.

6.5.5 5. Preserves Interface Simplicity

Avoiding function name proliferation keeps the interface clean and teachable.

6.6 NTTP Overloads Not Included

We also considered adding non-type template parameter (NTTP) overloads to std::string_view:

However, we decided against including these for several reasons:

  1. Core goal achieved without them: The primary objective of interface unification is
    accomplished by the runtime functions alone
  2. Limited practical benefit for string_view: Unlike span<T, N>, string_view is
    always runtime-sized, so NTTP overloads don’t provide compile-time bounds checking
  3. Scope creep: Adding NTTP overloads expands the proposal beyond the essential
    unification goal
  4. Future compatibility: These can be added later if compelling use cases emerge,
    without breaking changes

6.7 Generic Programming with Mixed Span Types

The chosen approach enables clean generic programming patterns:

template<typename View>
void trim_prefix(View& view, size_t n) {
    if constexpr (requires { view.remove_prefix(n); }) {
        // Works for string_view and dynamic span
        view.remove_prefix(n);
    } else {
        // Fallback for fixed span - reassignment
        view = view.subspan(n);
    }
}

// For cases where you know the view supports modification:
template<typename ModifiableView>
  requires requires(ModifiableView& v) { v.remove_prefix(1); }
void trim_prefix_simple(ModifiableView& view) {
    view.remove_prefix(1);  // Always modifies in-place
}

This approach provides:

6.8 Semantics and Exception Safety

The new functions follow the same semantics and exception guarantees as their existing counterparts:

6.9 Naming Consistency

We maintain existing naming conventions:

6.10 Performance Considerations

The new functions are implemented as simple forwarding operations with no performance overhead:

// string_view::first implementation
constexpr string_view first(size_type n) const {
    return substr(0, n);
}

// span::remove_prefix implementation  
constexpr void remove_prefix(size_type n) {
    *this = subspan(n);
}

7 Implementation Experience

Several implementations have been prototyped, confirming:

  1. Trivial implementation complexity - each function is a simple one-liner
  2. Zero performance overhead - optimizes to identical assembly
  3. No ABI impact - new functions don’t affect existing symbols
  4. High user satisfaction - dramatically improves generic programming experience

8 Wording

The proposed changes are relative to [@!N5008].

8.1 Additions to [string.view.syn]

namespace std {
  template<class charT, class traits = char_traits<charT>>
  class basic_string_view {
    // ...
    constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
+   constexpr basic_string_view first(size_type n) const;
+   constexpr basic_string_view last(size_type n) const;
    
    constexpr void remove_prefix(size_type n);
    constexpr void remove_suffix(size_type n);
    // ...
  };
}

8.2 Additions to [span.syn]

namespace std {
  template<class ElementType, size_t Extent = dynamic_extent>
  class span {
    // ...
    template<size_t Count> constexpr span<element_type, Count> first() const;
    constexpr span<element_type, dynamic_extent> first(size_type count) const;
    template<size_t Count> constexpr span<element_type, Count> last() const;
    constexpr span<element_type, dynamic_extent> last(size_type count) const;
    // ...
  };
  
+ // Additional members for dynamic extent spans only
+ template<class ElementType>
+ class span<ElementType, dynamic_extent> {
+   // ... (all existing members)
+   constexpr void remove_prefix(size_type n);
+   constexpr void remove_suffix(size_type n);
+ };
}

8.3 Function Specifications

8.3.1 [string.view.ops] Additions

+constexpr basic_string_view first(size_type n) const;

Preconditions: n <= size().

Returns: substr(0, n).

+constexpr basic_string_view last(size_type n) const;

Preconditions: n <= size().

Returns: substr(size() - n, n).

8.3.2 [span.elem] Additions (Dynamic Extent Only)

Note: The following member functions are only available for span<ElementType, dynamic_extent>.

+constexpr void remove_prefix(size_type n);

Preconditions: n <= size().

Effects: Equivalent to: *this = subspan(n);.

+constexpr void remove_suffix(size_type n);

Preconditions: n <= size().

Effects: Equivalent to: *this = subspan(0, size() - n);.

8.4 Feature test macro

Add the following feature test macro to [version.syn]:

+#define __cpp_lib_unified_span_string_view 202509L // also in <span> and <string_view>

9 Future Considerations

This proposal lays the groundwork for:

  1. Enhanced generic algorithms in the standard library
  2. Simplified range adaptors that work uniformly with both types
  3. Potential concept definitions for view-like types
  4. Teaching materials that can present a unified mental model

The unified interface also enables future proposals for:

10 Acknowledgements

Special thanks and recognition goes to The C++ Alliance for supporting Mateusz’s membership in the ISO C++ Committee and the production of this proposal.

Additional thanks to the C++ community for feedback on interface consistency issues and generic programming challenges with the current standard library.