From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6288 invoked by alias); 24 Oct 2017 18:25:31 -0000 Mailing-List: contact libstdc++-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libstdc++-owner@gcc.gnu.org Received: (qmail 6238 invoked by uid 89); 24 Oct 2017 18:25:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=basic_fstream, basic_ifstream, basic_ofstream, 4879 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 24 Oct 2017 18:25:29 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 85DE810F2E; Tue, 24 Oct 2017 18:25:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 85DE810F2E Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jwakely@redhat.com Received: from localhost (unknown [10.33.36.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 269AA6BF73; Tue, 24 Oct 2017 18:25:28 +0000 (UTC) Date: Tue, 24 Oct 2017 18:25:00 -0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: RFC: why do our std::ofsteam functions add ios_base::trunc ? Message-ID: <20171024182527.GY3297@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="zjcmjzIkjQU2rmur" Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.1 (2017-09-22) X-IsSubscribed: yes X-SW-Source: 2017-10/txt/msg00085.txt.bz2 --zjcmjzIkjQU2rmur Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-length: 700 While adding the new C++17 overloads taking filesystem paths to I noticed that ofstream uses out|trunc for the default arguments instead of out as the stadnard says. The effect is the same, but my preference would be to do what the standard says. Is there a good reason for deviating from what the standard says? Although the out|trunc default arguments have no difference in behaviour from just using out, the comments saying "Calls @c std::basic_filebuf::open(__s,__mode|out|trunc)" are definitely wrong. That would not be a conforming behaviour. Also, I think we can remove the comments saying to use c_str() as fstreams accept std::string arguments since C++11. Any comments? --zjcmjzIkjQU2rmur Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" Content-length: 4879 diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream index 52830945fe2..db41a697085 100644 --- a/libstdc++-v3/include/std/fstream +++ b/libstdc++-v3/include/std/fstream @@ -487,9 +487,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __mode Open file in specified mode (see std::ios_base). * * @c ios_base::in is automatically included in @a __mode. - * - * Tip: When using std::string to hold the filename, you must use - * .c_str() before passing it to this constructor. */ explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) @@ -587,9 +584,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * * Calls @c std::basic_filebuf::open(s,__mode|in). If that function * fails, @c failbit is set in the stream's error state. - * - * Tip: When using std::string to hold the filename, you must use - * .c_str() before passing it to this constructor. */ void open(const char* __s, ios_base::openmode __mode = ios_base::in) @@ -687,15 +681,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __s Null terminated string specifying the filename. * @param __mode Open file in specified mode (see std::ios_base). * - * @c ios_base::out | @c ios_base::trunc is automatically included in - * @a __mode. - * - * Tip: When using std::string to hold the filename, you must use - * .c_str() before passing it to this constructor. + * @c ios_base::out is automatically included in @a __mode. */ explicit basic_ofstream(const char* __s, - ios_base::openmode __mode = ios_base::out|ios_base::trunc) + ios_base::openmode __mode = ios_base::out) : __ostream_type(), _M_filebuf() { this->init(&_M_filebuf); @@ -708,12 +698,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __s std::string specifying the filename. * @param __mode Open file in specified mode (see std::ios_base). * - * @c ios_base::out | @c ios_base::trunc is automatically included in - * @a __mode. + * @c ios_base::out is automatically included in @a __mode. */ explicit basic_ofstream(const std::string& __s, - ios_base::openmode __mode = ios_base::out|ios_base::trunc) + ios_base::openmode __mode = ios_base::out) : __ostream_type(), _M_filebuf() { this->init(&_M_filebuf); @@ -789,15 +778,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __s The name of the file. * @param __mode The open mode flags. * - * Calls @c std::basic_filebuf::open(__s,__mode|out|trunc). If that + * Calls @c std::basic_filebuf::open(__s,__mode|out). If that * function fails, @c failbit is set in the stream's error state. - * - * Tip: When using std::string to hold the filename, you must use - * .c_str() before passing it to this constructor. */ void - open(const char* __s, - ios_base::openmode __mode = ios_base::out | ios_base::trunc) + open(const char* __s, ios_base::openmode __mode = ios_base::out) { if (!_M_filebuf.open(__s, __mode | ios_base::out)) this->setstate(ios_base::failbit); @@ -813,12 +798,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __s The name of the file. * @param __mode The open mode flags. * - * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that + * Calls @c std::basic_filebuf::open(s,mode|out). If that * function fails, @c failbit is set in the stream's error state. */ void - open(const std::string& __s, - ios_base::openmode __mode = ios_base::out | ios_base::trunc) + open(const std::string& __s, ios_base::openmode __mode = ios_base::out) { if (!_M_filebuf.open(__s, __mode | ios_base::out)) this->setstate(ios_base::failbit); @@ -894,9 +878,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @brief Create an input/output file stream. * @param __s Null terminated string specifying the filename. * @param __mode Open file in specified mode (see std::ios_base). - * - * Tip: When using std::string to hold the filename, you must use - * .c_str() before passing it to this constructor. */ explicit basic_fstream(const char* __s, @@ -993,9 +974,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * * Calls @c std::basic_filebuf::open(__s,__mode). If that * function fails, @c failbit is set in the stream's error state. - * - * Tip: When using std::string to hold the filename, you must use - * .c_str() before passing it to this constructor. */ void open(const char* __s, --zjcmjzIkjQU2rmur--