[LLVMbugs] [Bug 20596] New: Non conformance caused by _Stream&& operator<<(_Stream&& __os, const _Tp& __x)

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Aug 8 12:27:30 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20596

            Bug ID: 20596
           Summary: Non conformance caused by _Stream&&
                    operator<<(_Stream&& __os, const _Tp& __x)
           Product: libc++
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: gpakosz at pempek.net
                CC: llvmbugs at cs.uiuc.edu, mclow.lists at gmail.com
    Classification: Unclassified

The following snippets compiles successfully with clang++ on MacOS X 10.9.4

$ clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

---
#include <string>
#include <iostream>
#include <sstream>

void print(const std::string& s)
{
  std::cout << s << std::endl;
}

int main()
{
  int v = 1;
  print((std::ostringstream() << "value: " << v).str());
  return 0;
}

The type of ostringstream() << "value: " is rvalue ostringstream.
While it should be lvalue ostream.

According to the addition of the following operator in C++11,
template< class CharT, class Traits, class T >
basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os, 
                                            const T& value );

The code sample above should be written the following way:
print(static_cast<std::ostringstream&>(std::ostringstream() << "value: " <<
v).str());

But then, clang++ with libc++ returns the following error message:
$ clang++ --std=c++11 -g main.cpp
main.cpp:13:9: error: non-const lvalue reference to type
'basic_ostringstream<[3 * ...]>' cannot bind to a temporary of type
'basic_ostringstream<[3 * ...]>'
  print(static_cast<std::ostringstream&>(std::ostringstream() << "value: " <<
v).str());


---
In the end, the most reliable way to achieve that one-liner is to write:
print(static_cast<std::ostringstream&>(std::ostringstream().flush() << "value:
" << v).str());

Now it compiles successfully and works as expected with G++, clang++ (libstdc++
or libc++) and also MSVC++ 2012. Either in C++2003 or C++11. Note that in the
case of MSVC++ 2012, I went for flush() instead of seekp(0) because seekp(0) on
a newly constructed ostringstream sets the failbit...

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140808/10288136/attachment.html>


More information about the llvm-bugs mailing list