|
| 1 | +--- |
| 2 | +title: "String literals considered harmful in ranges" |
| 3 | +document: DXXXXR0 |
| 4 | +date: 2025-11-09 |
| 5 | +audience: SG9 |
| 6 | +author: |
| 7 | + - name: Hui Xie |
| 8 | + |
| 9 | + - name: Jonathan Müller |
| 10 | + |
| 11 | +toc: true |
| 12 | +--- |
| 13 | + |
| 14 | +# Revision History |
| 15 | + |
| 16 | +## R0 |
| 17 | + |
| 18 | +- Initial revision |
| 19 | + |
| 20 | +# Abstract |
| 21 | + |
| 22 | +This paper proposes to fix string literal support in ranges library |
| 23 | + |
| 24 | +# Introduction |
| 25 | + |
| 26 | + |
| 27 | +#### `ranges::empty` |
| 28 | + |
| 29 | +```cpp |
| 30 | +std::ranges::empty(""sv); // true |
| 31 | +std::ranges::empty(""s); // true |
| 32 | +std::ranges::empty(""); // false |
| 33 | +``` |
| 34 | +
|
| 35 | +#### `ranges::size` |
| 36 | +
|
| 37 | +```cpp |
| 38 | +std::ranges::size("abc"sv); // 3 |
| 39 | +std::ranges::size("abc"s); // 3 |
| 40 | +std::ranges::size("abc"); // 4 |
| 41 | +``` |
| 42 | + |
| 43 | +#### `views::split/lazy_split` |
| 44 | + |
| 45 | +```cpp |
| 46 | +std::views::split("ab cd ef"sv, " "sv) // split into 3 elements |
| 47 | +std::views::split("ab cd ef"sv, " "s) // split into 3 elements |
| 48 | +std::views::split("ab cd ef"sv, " ") // split into 1 element |
| 49 | +``` |
| 50 | +
|
| 51 | +#### `views::concat` |
| 52 | +
|
| 53 | +```cpp |
| 54 | +std::views::concat("ab"sv, "cd"sv); // abcd |
| 55 | +std::views::concat("ab"s, "cd"s); // abcd |
| 56 | +std::views::concat("ab", "cd"); // ab\0cd\0 |
| 57 | +``` |
| 58 | + |
| 59 | +#### `views::reverse` |
| 60 | + |
| 61 | +```cpp |
| 62 | +"abc"sv | std::views::reverse; // cba |
| 63 | +"abc"s | std::views::reverse; // cba |
| 64 | +"abc" | std::views::reverse; // \0cba |
| 65 | +``` |
| 66 | + |
| 67 | +#### `views::transform` |
| 68 | + |
| 69 | +```cpp |
| 70 | +auto atoi_leetcoder = [](char c) -> int { return c - '0'; }; |
| 71 | + |
| 72 | +"123"sv | std::views::transform(atoi_leetcoder); // [1,2,3] |
| 73 | +"123"s | std::views::transform(atoi_leetcoder); // [1,2,3] |
| 74 | +"123" | std::views::transform(atoi_leetcoder); // [1,2,3,-48] |
| 75 | +``` |
| 76 | +
|
| 77 | +#### `views::cartesian_product` |
| 78 | +
|
| 79 | +```cpp |
| 80 | +std::views::cartesian_product("ab"sv, "cd"sv); // ac, ad, bc, bd |
| 81 | +std::views::cartesian_product("ab"s, "cd"s); // ac, ad, bc, bd |
| 82 | +std::views::cartesian_product("ab", "cd"); // ac, ad, a\0, bc, bd, b\0, \0c, \0d, \0\0 |
| 83 | +``` |
| 84 | + |
| 85 | +# Proposed Solutions |
| 86 | + |
| 87 | +## Deprecate String Literals as non-`viewable_range` or `view::all` |
| 88 | + |
| 89 | +The goal is to make the usage ill-formed (with diagnostic). The usual |
| 90 | +procedure to invalidate existing valid code is going through the deprecation |
| 91 | +path. An obvious choice to make string literals not usable with ranges library |
| 92 | +is to make it not a `viewable_range`. However, this can't achieved by using |
| 93 | +`[[deprecated]]` attribute. Another solution is to have a new overload in |
| 94 | +`views::all` and mark that overload `[[deprecated]]` |
| 95 | + |
| 96 | +## Make `view::all` to return `string_view` for String Literals |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## How to detect a string literal |
| 101 | + |
| 102 | +`"ab"` has the same type as `const char c[] = {'a', 'b', 'c'}`. There is no simple |
| 103 | +way to detect it just through the type system. |
| 104 | + |
| 105 | + |
| 106 | +# Wording |
| 107 | + |
| 108 | +# Implementation Experience |
| 109 | + |
| 110 | +# Feature Test Macro |
| 111 | + |
| 112 | + |
| 113 | +<style> |
| 114 | +.bq{ |
| 115 | + display: block; |
| 116 | + margin-block-start: 1em; |
| 117 | + margin-block-end: 1em; |
| 118 | + margin-inline-start: 40px; |
| 119 | + margin-inline-end: 40px; |
| 120 | +} |
| 121 | +</style> |
0 commit comments