Bug 13478 - [3.3 Regression] gcc uses wrong constructor to initialize a const reference
Summary: [3.3 Regression] gcc uses wrong constructor to initialize a const reference
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 3.3.3
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2003-12-23 14:33 UTC by erich günther
Modified: 2004-01-21 07:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-01-11 12:24:53


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description erich günther 2003-12-23 14:33:04 UTC
#define FAILS
/* gcc (GCC) 3.3 20030226 (prerelease) (SuSE Linux)
tzst2.cpp: In function `void foo(const SA*)':
tzst2.cpp:29: error: `CA::CA(const CA&)' is private
tzst2.cpp:34: error: within this context
tzst2.cpp:34: error:   initializing temporary from result of `CA::CA(const SA&)
*/

// Data layout only
struct SA
{
};


class CA :protected SA
{
public:
	CA()
	{
	};

	CA(const SA& a)
	{
	};


#ifdef FAILS
	private:
	CA(const CA& a);
#endif
};

void foo(const SA * pa)
{
	const CA& ra=*pa;
}
Comment 1 Andrew Pinski 2003-12-23 18:13:02 UTC
The error is right you have to an accessorable copy constructor when initing a variable.
Comment 2 Giovanni Bajo 2003-12-24 02:40:10 UTC
That's not a variable, it's a reference. GCC should not call any constructor 
when binding the variable to that reference.
Comment 3 Giovanni Bajo 2003-12-24 02:55:19 UTC
Uhm, ok, it must call a copy constructor, but the first one.
Comment 4 Giovanni Bajo 2004-01-11 12:24:52 UTC
Ok. Given:

------------------------------------
struct A {};
struct B : protected A {
    B() {};
    B(const A& ) {};
private:
    B(const B& ) {};
};

void foo(const A* ap)
{
  const B& br = *ap;
}
------------------------------------

The code should compile, because it should call B(const A&) to construct the 
temporary which is then bound to the const reference. Instead, GCC seems to 
call B(const B&), which is private. I can't test on 3.0/3.1, but it's surely a 
regression since 3.0.4, and can be made a wrong-code one if we make the 
constructor public. I keep it as rejects-valid as it's easier to debug.
Comment 5 Andrew Pinski 2004-01-15 05:36:38 UTC
Failing since at least 2000-12-31.
Comment 6 GCC Commits 2004-01-16 16:59:35 UTC
Subject: Bug 13478

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2004-01-16 16:59:30

Modified files:
	gcc/cp         : ChangeLog call.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/g++.dg/init: ref10.C 

Log message:
	PR c++/13478
	* call.c (convert_like_real): Do not perform an additional
	direct-initialization when binding to a reference.
	
	PR c++/13478
	* g++.dg/init/ref10.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3887&r2=1.3888
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.449&r2=1.450
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3384&r2=1.3385
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/ref10.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 7 GCC Commits 2004-01-16 17:54:31 UTC
Subject: Bug 13478

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2004-01-16 17:54:25

Modified files:
	gcc/cp         : ChangeLog call.c 

Log message:
	PR c++/13478
	* call.c (initialize_reference): Pass -1 for inner parameter to
	convert_like_real.
	
	PR c++/13478
	* g++.dg/init/ref10.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3888&r2=1.3889
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.450&r2=1.451

Comment 8 Mark Mitchell 2004-01-16 17:57:21 UTC
Fixed in GCC 3.4.
Comment 9 GCC Commits 2004-01-21 07:30:11 UTC
Subject: Bug 13478

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	gdr@gcc.gnu.org	2004-01-21 07:30:07

Modified files:
	gcc/cp         : ChangeLog call.c 
Added files:
	gcc/testsuite/g++.dg/init: ref10.C 

Log message:
	Backport from mainline
	2004-01-16  Mark Mitchell  <mark@codesourcery.com>
	
	PR c++/13478
	* call.c (initialize_reference): Pass -1 for inner parameter to
	convert_like_real.

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.240&r2=1.3076.2.241
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.35&r2=1.341.2.36
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/ref10.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.6.1

Comment 10 Gabriel Dos Reis 2004-01-21 07:31:09 UTC
Fixed with a backport from mainline.