Bug 5116

Summary: operator resolution in template class
Product: gcc Reporter: binder
Component: c++Assignee: Nathan Sidwell <nathan>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs
Priority: P3 Keywords: wrong-code
Version: 3.0.2   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: bug1.cc

Description binder 2001-12-14 07:56:00 UTC
In the attached code g++ invokes operator bool() of the template class before the invokation of operator<<() takes place. 

This results in printing "1 in operator<<....." to stdout instead of printing "in operator<<..." twice.


#include <iostream>
using namespace std;

template <class T> struct Handle
{
	Handle(T* p)
	{}
	
	operator bool() const { return true; }
	
	friend ostream& operator<<(ostream& ostr, const Handle& r)
	{
		return ostr << "in operator<<(ostream&, const Handle&)";
	}
};

struct Buggy
{
	Buggy()
	{}
};

typedef Handle<Buggy>	Buggy_h;

struct BuggyCmp
{
	bool operator()(const Buggy_h& b1, const Buggy_h& b2) const
	{
		cout << b1 << " " << b2 << endl;
		return false;
	}
};


int main()
{
	BuggyCmp	cmp;

	cmp(new Buggy(), new Buggy());
}

Release:
3.0.2

Environment:
g++ version:

Reading specs from /home/binder2/gcc3/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.0.2/specs
Configured with: ../gcc-3.0.2-src/configure --prefix=/home/binder2/gcc --enable-shared --enable-threads=posix
Thread model: posix
gcc version 3.0.2

OS: RedHat Linux 7.2, kernel 2.4.16


libc version:

GNU C Library stable release version 2.2.4, by Roland McGrath et al.
Copyright (C) 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 2.96 20000731 (Red Hat Linux 7.1 2.96-98).
Compiled on a Linux 2.4.9-0.17smp system on 2001-10-03.
Available extensions:
        GNU libio by Per Bothner
        crypt add-on version 2.1 by Michael Glad and others
        The C stubs add-on version 2.1.2.
        linuxthreads-0.9 by Xavier Leroy
        BIND-8.2.3-T5B
        NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
        Glibc-2.0 compatibility add-on by Cristian Gafton 
        libthread_db work sponsored by Alpha Processor Inc
Report bugs using the `glibcbug' script to <bugs@gnu.org>.

How-To-Repeat:
compile and run the attached program
Comment 1 Nathan Sidwell 2001-12-30 15:43:51 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: confirmed
Comment 2 Nathan Sidwell 2001-12-31 07:16:13 UTC
Responsible-Changed-From-To: unassigned->nathan
Responsible-Changed-Why: patch in progress
Comment 3 Nathan Sidwell 2002-01-02 03:38:03 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: 2002-01-02  Nathan Sidwell  <nathan@codesourcery.com>
    
    	PR c++/5116, c++/764
    	* call.c (build_new_op): Make sure template class operands are
    	instantiated. Simplify arglist construction.
Comment 4 Nathan Sidwell 2002-01-02 06:33:13 UTC
State-Changed-From-To: closed->analyzed
State-Changed-Why: oops that patch is incorrect.
Comment 5 Nathan Sidwell 2002-12-26 04:27:25 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: 2002-12-26  Nathan Sidwell  <nathan@codesourcery.com>
    
    	PR c++/5116, c++/764
    	* call.c (build_new_op): Make sure template class operands are
    	instantiated.
Comment 6 Nathan Sidwell 2002-12-26 12:23:12 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/5116
Date: 26 Dec 2002 12:23:12 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	nathan@gcc.gnu.org	2002-12-26 04:23:12
 
 Modified files:
 	gcc/cp         : ChangeLog call.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/template: conv5.C friend10.C 
 
 Log message:
 	cp:
 	PR c++/5116, c++/764
 	* call.c (build_new_op): Make sure template class operands are
 	instantiated.
 	testsuite:
 	* g++.dg/template/friend10.C: New test.
 	* g++.dg/template/conv5.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3091&r2=1.3092
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.345&r2=1.346
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2277&r2=1.2278
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/conv5.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/friend10.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 
Comment 7 Nathan Sidwell 2002-12-26 12:28:26 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/5116
Date: 26 Dec 2002 12:28:26 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	nathan@gcc.gnu.org	2002-12-26 04:28:26
 
 Modified files:
 	gcc/cp         : ChangeLog call.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/template: conv5.C friend10.C 
 
 Log message:
 	cp:
 	PR c++/5116, c++/764
 	* call.c (build_new_op): Make sure template class operands are
 	instantiated.
 	testsuite:
 	* g++.dg/template/friend10.C: New test.
 	* g++.dg/template/conv5.C: New test.
 
 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.8&r2=1.3076.2.9
 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.1&r2=1.341.2.2
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.8&r2=1.2261.2.9
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/conv5.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/friend10.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1