Bug 8700

Summary: [3.2 regression] unhelpful error message for binding temp to reference
Product: gcc Reporter: adelroy
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Severity: normal CC: gcc-bugs, jbuck, mmitchel
Priority: P3    
Version: 3.2   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: txt.ii.bz2

Description adelroy 2002-11-25 07:16:03 UTC
Fails to match a unique operator<<, lists two copies as if they were ambiguous - but the line numbers are the same: definitely only defined once, only happens for temporary variable.

Get this error compiling following code...

tkrdev1:~dev/tpcc/error_contexts>  g++ -o txt txt.cc
txt.cc: In function `int main()':
txt.cc:28: no match for `txt << const char[21]' operator
txt.cc:13: candidates are: txt& operator<<(txt&, const V&) [with V = char[21]]
txt.cc:13:                 txt& operator<<(txt&, const V&) [with V = char[21]]
zsh: exit 1     g++ -o txt txt.cc
tkrdev1:~dev/tpcc/error_contexts>  cat txt.cc
#include <iostream>
#include <sstream>

using namespace std;

class txt
    ostringstream s;

    template<typename V>
    friend txt& operator<<(txt& t, const V& v)
        t.s << v;

        return t;

    operator const char*() { return s.str().c_str(); }

int main()
    txt t;

    cout << (t << "hello world!") << endl;   // ok

    cout << (txt() << "goodbye cruel world!") << endl; //not

    return 0;


 tkrdev1:~dev/tpcc/shmew>  g++ -v
Reading specs from /bb/home/adelroy/lib/gcc-lib/sparc-sun-solaris2.8/3.2/specs
Configured with: ../gcc-3.2/configure --prefix=/bb/home/adelroy --enable-languages=c,c++ --with-ld=/usr/ccs/bin/ld
Thread model: posix
gcc version 3.2

gcc version 3.2

Solaris 2.8 / Sparc

Try to compile code snippet provided in description.
Comment 1 adelroy 2002-11-25 07:16:03 UTC
Fixed in GCC 3.3, 3.4 with:

Comment 2 Wolfgang Bangerth 2002-11-25 08:53:59 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed. The reason, of course, is that you try to bind
    the temporary to a non-const reference. gcc2.95 actually
    tells you this:
    tmp/g> c++ -c x.cc
    x.cc: In function `int main()':
    x.cc:28: initialization of non-const reference type `class txt &'
    x.cc:28: from rvalue of type `txt'
    x.cc:13: in passing argument 1 of `operator <<<char[21]>(txt &, const char (&)[21])'
    The messages we get from 3.2.1 and 3.3 are incomprehensible
    compared to that, which I rate as a regression, so I put
    this into "high" state and leave it to others whether this
    merits fixing.
Comment 3 Mark Mitchell 2003-03-11 15:43:15 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Subject: c++/8700
Date: 11 Mar 2003 15:43:15 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	mmitchel@gcc.gnu.org	2003-03-11 15:43:15
 Modified files:
 	gcc/cp         : ChangeLog call.c cp-tree.h decl2.c friend.c 
 Log message:
 	PR c++/8700
 	* call.c (convert_class_to_reference): Adjust usage of
 	(any_viable): Remove.
 	(splice_viable): Combine with any_viable.
 	(print_z_candidates): Avoid printing duplicates.
 	(build_user_type_conversion_1): Adjust usage of splice_viable.
 	(build_new_function_call): Likewise.
 	(build_operator_new_call): Likewise.
 	(build_object_call): Likewise.
 	(build_conditional_expr): Likewise.
 	(build_new_op): Likewise.
 	(build_new_method_call): Likewise.
 	(joust): Remove spurious comment.
 	* cp-tree.h (DECL_FRIENDLIST): Correct documentation.
 	* decl2.c (arg_assoc_class): Simplify.
 	* friend.c (add_friend): Likewise.

Comment 4 Mark Mitchell 2003-03-11 15:56:52 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Subject: c++/8700
Date: 11 Mar 2003 15:56:52 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	mmitchel@gcc.gnu.org	2003-03-11 15:56:52
 Modified files:
 	gcc/cp         : ChangeLog call.c 
 Log message:
 	PR c++/8700
 	* call.c (print_z_candidates): Avoid printing duplicates.
Comment 5 Joe Buck 2003-04-25 19:47:35 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed for 3.3 and in trunk, and 3.2 branch is now closed.