diff mbox

[PR56193] - Wrong test operator for basic_ios in C++11

Message ID 5113BB65.2050501@verizon.net
State New
Headers show

Commit Message

Ed Smith-Rowland Feb. 7, 2013, 2:34 p.m. UTC
On 02/07/2013 04:18 AM, Jonathan Wakely wrote:
> On 7 February 2013 03:05, Ed Smith-Rowland wrote:
>> greetings,
>>
>> On SO someone noticed that g++ still has in the basic_ios class
>>    operator void*();
>> instead of
>>    explicit operator bool() const;
>> The old C++98 operator allows things like
>>    std::cout << std::cout;
>>
>> This patch changes the stream test operator for C++11 and onwards.
>>
>> This patch builds and tests clean on x86_64-unknown-linux.
> Please use -std=gnu++11 in the test's dg-options and CC the
> gcc-patches list too.
> OK for trunk with that change, thanks.
>
Attached patch applied.
2013-02-06  Edward Smith-Rowland  <3dw4rd@verizon.net>

	* include/bits/basic_ios.h: Replace operator void*() const
	with explicit operator bool() const in C++11 and greater.
	* testsuite/27_io/basic_ios/pr56193.cc: New file.
diff mbox

Patch

Index: include/bits/basic_ios.h
===================================================================
--- include/bits/basic_ios.h	(revision 195747)
+++ include/bits/basic_ios.h	(working copy)
@@ -112,8 +112,13 @@ 
        *  This allows you to write constructs such as
        *  <code>if (!a_stream) ...</code> and <code>while (a_stream) ...</code>
       */
+#if __cplusplus >= 201103L
+      explicit operator bool() const
+      { return !this->fail(); }
+#else
       operator void*() const
       { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
+#endif
 
       bool
       operator!() const
Index: testsuite/27_io/basic_ios/pr56193.cc
===================================================================
--- testsuite/27_io/basic_ios/pr56193.cc	(revision 0)
+++ testsuite/27_io/basic_ios/pr56193.cc	(revision 0)
@@ -0,0 +1,15 @@ 
+// { dg-do compile }
+// { dg-options "-std=gnu++11" }
+// Copyright (C) 2013 Free Software Foundation, Inc.
+
+#include <iostream>
+
+// PR libstdc++/56193
+
+int
+test01()
+{
+  std::cout << std::cout; // { dg-error "cannot bind" }
+}
+
+// { dg-error "initializing argument" "" { target *-*-* } 602 }