From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id AE1B338582A1 for ; Thu, 23 Jun 2022 21:05:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AE1B338582A1 Received: by mail-ej1-x62f.google.com with SMTP id lw20so747883ejb.4 for ; Thu, 23 Jun 2022 14:05:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:reply-to:from:date:message-id :subject:to; bh=ib/HOCuRrFUCgKlGKD4d1bhQbtHGVBwnNq6vN+bv7pM=; b=KT/RCFR2fn0KI/nTlvlmupIjaOe9dV91YgP0qMBTxqREHEgJqZuM17rFWfRhOkUVj/ sHPZKZx8gMasUFucmRwKzos7SoboKqvEbHFYDHJb+MlktR0q0Upeh0sXz9dNeucOVIlL mZpTLH5AcwQt2RDKk5AXT+r5V863ppUNEY5eaosL6VS6tbrz0uG4xE5oTdQomgdctTjs GSvmbgKGCr8KBSCq3MpGS6Vi64NcBORijksWlIZ5alC91xUxDdTvVad/ZMclNSp25fPM iFNW/yFp8Wxua5Q2vUfXzNj52gVXze0U+zlWZehP356rnPSUWgJyYgCE6COKanDZy1ey T+8A== X-Gm-Message-State: AJIora+Jh3aiMkuv0RuOmg2zmJiH3UTFAk7mcb1P2HqLwcmtHS1RMA62 XVzTlybNFTK2j9tymMjKSXU3HLq1/zpEKTQVfmxrT0kGS7k= X-Google-Smtp-Source: AGRyM1s4FxG0bzQpHLQRZoQceT5h8Mfj6ViX/C5RAl8vYzp9RtTKOKAjCIzCtV1IgODL7LidH4kKeM+9l0OhTlKz4Pk= X-Received: by 2002:a17:906:dc8f:b0:723:a62a:cd0e with SMTP id cs15-20020a170906dc8f00b00723a62acd0emr5958242ejc.444.1656018333134; Thu, 23 Jun 2022 14:05:33 -0700 (PDT) MIME-Version: 1.0 Reply-To: cauldwell.thomas@gmail.com From: Frederick Virchanza Gotham Date: Thu, 23 Jun 2022 22:05:22 +0100 Message-ID: Subject: string::iterator should have more error checking To: libstdc++@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jun 2022 21:05:36 -0000 If a program is compiled with "-D_GLIBCXX_DEBUG", I would expect it at runtime to catch the error on the last line in the following program: #include #include #include #include using namespace std; int main(void) { cout << "string::const_iterator is " << (is_same_v< string::const_iterator, char const * > ? "just a raw pointer" : "NOT a simple pointer") << endl; cout << "string_view::const_iterator is " << (is_same_v< string_view::const_iterator, char const * > ? "just a raw pointer" : "NOT a simple pointer") << endl; string s("brush"); cout << string_view( &*(s.cbegin() + 1u), &*(s.cend() + 876u) ) << endl; } string::iterator is NOT a simple pointer -- it is a class and so we can overload the following operators to catch errors: (1) unary operator* (2) binary operator+ (3) binary operator- The error on the last line of the above program would be caught at runtime if the iterator were written as follows: class string { class iterator { char const *const p_min, *const p_max; // initialised in constructor char *p; public: iterator &operator+(ptrdiff_t const n) { assert( p+n >= p_min ); assert( p+n <= p_max ); // more code here } }; }; Similarly the unary operator* could be overloaded to catch the error when "end()" gets dereferenced.