https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80662

            Bug ID: 80662
           Summary: libstdc++ basic_string casting oddity
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dje at gcc dot gnu.org
  Target Milestone: ---
            Target: x86-64

// https://twitter.com/zxovq/status/861377261827629057
#include <sstream>
#include <string>

class my_stream : public std::stringstream {
public:
    operator std::string() const
    {
        return std::stringstream::str();
    }

    template<typename T>
    my_stream& operator<<(T&& value)
    {
        std::stringstream::operator<<(std::forward<T>(value));
        return *this;
    }
};

int main(void)
{
    // for an unknown reason gcc 7.1 implicitly casts (my_stream{} << "")
    // to std::basic_ostream& instead of using my_stream&
    // gcc 6.3 is fine; uncomment static_cast below to make gcc 7.1 work
    std::string s{/*static_cast<my_stream&>*/(my_stream{} << "hello world")};
}


<source>: In function 'int main()':
<source>:24:76: error: no matching function for call to
'std::__cxx11::basic_string<char>::basic_string(<brace-enclosed initializer
list>)'
     std::string s{/*static_cast<my_stream&>*/(my_stream{} << "hello world")};
                                                                            ^
In file included from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/string:52:0,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/locale_classes.h:40,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/ios_base.h:41,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/ios:42,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/istream:38,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/sstream:38,
                 from <source>:1:
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:588:9:
note: candidate: template<class _InputIterator, class>
std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&)
         basic_string(_InputIterator __beg, _InputIterator __end,
         ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:588:9:
note:   template argument deduction/substitution failed:
<source>:24:76: note:   candidate expects 3 arguments, 1 provided
     std::string s{/*static_cast<my_stream&>*/(my_stream{} << "hello world")};
                                                                            ^
In file included from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/string:52:0,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/locale_classes.h:40,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/ios_base.h:41,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/ios:42,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/istream:38,
                 from
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/sstream:38,
                 from <source>:1:
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:550:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&,
const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]
       basic_string(basic_string&& __str, const _Alloc& __a)
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:550:7:
note:   candidate expects 2 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:546:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>;
_Alloc = std::allocator<char>]
       basic_string(const basic_string& __str, const _Alloc& __a)
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:546:7:
note:   candidate expects 2 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:542:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:542:7:
note:   no known conversion for argument 1 from 'std::basic_ostream<char>' to
'std::initializer_list<char>'
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:515:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)
[with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]
       basic_string(basic_string&& __str) noexcept
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:515:7:
note:   no known conversion for argument 1 from 'std::basic_ostream<char>' to
'std::__cxx11::basic_string<char>&&'
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:503:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::size_type, _CharT, const _Alloc&) [with _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>;
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned
int]
       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:503:7:
note:   candidate expects 3 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:493:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char;
_Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:493:7:
note:   no known conversion for argument 1 from 'std::basic_ostream<char>' to
'const char*'
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:483:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT,
_Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>;
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned
int]
       basic_string(const _CharT* __s, size_type __n,
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:483:7:
note:   candidate expects 3 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:465:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type,
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&)
[with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::size_type = long unsigned int]
       basic_string(const basic_string& __str, size_type __pos,
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:465:7:
note:   candidate expects 4 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:449:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type,
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT =
char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>;
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned
int]
       basic_string(const basic_string& __str, size_type __pos,
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:449:7:
note:   candidate expects 3 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:434:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const
_Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::size_type = long unsigned int]
       basic_string(const basic_string& __str, size_type __pos,
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:434:7:
note:   candidate expects 3 arguments, 1 provided
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:421:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc =
std::allocator<char>]
       basic_string(const basic_string& __str)
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:421:7:
note:   no known conversion for argument 1 from 'std::basic_ostream<char>' to
'const std::__cxx11::basic_string<char>&'
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:413:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits =
std::char_traits<char>; _Alloc = std::allocator<char>]
       basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:413:7:
note:   no known conversion for argument 1 from 'std::basic_ostream<char>' to
'const std::allocator<char>&'
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:404:7:
note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>;
_Alloc = std::allocator<char>]
       basic_string()
       ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-7.1.0/include/c++/7.1.0/bits/basic_string.h:404:7:
note:   candidate expects 0 arguments, 1 provided
Compiler exited with result code 1

Reply via email to