public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* RFC: why do our std::ofsteam functions add ios_base::trunc ?
@ 2017-10-24 18:25 Jonathan Wakely
  2017-10-30 15:36 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2017-10-24 18:25 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 700 bytes --]

While adding the new C++17 overloads taking filesystem paths to
<fstream> 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?



[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 4879 bytes --]

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,

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: RFC: why do our std::ofsteam functions add ios_base::trunc ?
  2017-10-24 18:25 RFC: why do our std::ofsteam functions add ios_base::trunc ? Jonathan Wakely
@ 2017-10-30 15:36 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2017-10-30 15:36 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 906 bytes --]

On 24/10/17 19:25 +0100, Jonathan Wakely wrote:
>While adding the new C++17 overloads taking filesystem paths to
><fstream> 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?

I've removed these redundant bits from the default arguments, and from
the misleading comments.

Tested powerpc64le-linux, committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 6206 bytes --]

commit 892871f7c6259919aecfb197ea69c013d9c19cb2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Oct 30 15:00:49 2017 +0000

    Remove ios_mode::trunc from basic_ofstream openmode arguments
    
            * include/std/fstream (basic_ifstream, basic_ofstream, basic_fstream):
            Remove outdated comments about calling c_str() to create a file stream
            from a std::string.
            (basic_ofstream::basic_ofstream, basic_ofstream::open): Remove
            redundant ios_mode::trunc bits from default arguments and comments.

diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream
index 3205f81fb47..a3324c004d7 100644
--- a/libstdc++-v3/include/std/fstream
+++ b/libstdc++-v3/include/std/fstream
@@ -506,9 +506,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)
@@ -622,9 +619,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)
@@ -738,15 +732,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);
@@ -759,12 +749,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);
@@ -777,13 +766,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  @param  __s  filesystem::path 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.
        */
       template<typename _Path, typename = _Require<
 	  is_constructible<__filebuf_type, const _Path&, ios_base::openmode>>>
-	basic_ofstream(const _Path& __s, ios_base::openmode __mode
-		       = ios_base::out|ios_base::trunc)
+	basic_ofstream(const _Path& __s,
+		       ios_base::openmode __mode = ios_base::out)
 	: basic_ofstream(__s.c_str(), __mode)
 	{ }
 #endif // C++17
@@ -857,15 +845,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);
@@ -881,12 +865,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);
@@ -978,9 +961,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,
@@ -1091,9 +1071,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,

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-10-30 15:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-24 18:25 RFC: why do our std::ofsteam functions add ios_base::trunc ? Jonathan Wakely
2017-10-30 15:36 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).