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>
Status: RESOLVED FIXED    
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;

  public:
    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

Release:
gcc version 3.2

Environment:
Solaris 2.8 / Sparc

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

http://gcc.gnu.org/ml/gcc-patches/2003-03/msg00974.html
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
Cc:  
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
 	splice_viable.
 	(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.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3261&r2=1.3262
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.366&r2=1.367
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&r1=1.823&r2=1.824
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl2.c.diff?cvsroot=gcc&r1=1.603&r2=1.604
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/friend.c.diff?cvsroot=gcc&r1=1.81&r2=1.82
 

Comment 4 Mark Mitchell 2003-03-11 15:56:52 UTC
From: mmitchel@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
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.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.74&r2=1.3076.2.75
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.341.2.11&r2=1.341.2.12
 
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.