Bug 14211 - [3.3/3.4/4.0 Regression] const_cast returns lvalue but should be rvalue
[3.3/3.4/4.0 Regression] const_cast returns lvalue but should be rvalue
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: c++
3.3.2
: P2 normal
: 3.4.1
Assigned To: Mark Mitchell
: accepts-invalid
: 15076 (view as bug list)
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2004-02-19 15:06 UTC by d.binderman
Modified: 2004-10-30 21:11 UTC (History)
3 users (show)

See Also:
Host: linux/i386
Target:
Build:
Known to work: 3.2.3
Known to fail: 3.3.3 3.4.0 4.0.0
Last reconfirmed: 2004-02-20 21:30:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description d.binderman 2004-02-19 15:06:30 UTC
// Given the following C++ code

void f( char * str)
{

	char * & m = const_cast<char *>( str);
}

/*
then g++ 332 says not a lot.

[dcb@localhost src]$ ~/gnu/gcc332/results/bin/g++ -c -g -O2 -Wall -ansi
-pedantic cc.cc
cc.cc: In function `void f(char*)':
cc.cc:7: warning: unused variable `char*&m'
[dcb@localhost src]$

But Intel C++ 8.0 says

[dcb@localhost src]$ icc cc.cc
cc.cc(7): error: initial value of reference to non-const must be an lvalue
        char * & m = const_cast<char *>( str);
                     ^

compilation aborted for cc.cc (code 2)
[dcb@localhost src]$

One compiler must be wrong. 

*/
Comment 1 Wolfgang Bangerth 2004-02-20 21:30:30 UTC
5.2.11/1 clearly states that the result of a const_cast<T> is of type T. 
In your case, it is a char*, so it is not an lvalue. Thus, gcc must 
be wrong. Note also 5.2.11/4. 
 
W. 
Comment 2 Giovanni Bajo 2004-02-23 01:42:52 UTC
Agreed, GCC is wrong here.
Comment 3 Giovanni Bajo 2004-06-06 03:36:58 UTC
Retargeting to 3.4.1, being a regression on that release branch.
Comment 4 Mark Mitchell 2004-06-09 19:11:46 UTC
Working on a fix.
Comment 5 CVS Commits 2004-06-10 14:26:32 UTC
Subject: Bug 14211

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2004-06-10 14:26:23

Modified files:
	gcc/cp         : ChangeLog parser.c pt.c typeck.c 
Added files:
	gcc/testsuite/g++.dg/conversion: const1.C reinterpret1.C 
	gcc/testsuite/g++.dg/template: enum2.C error13.C 

Log message:
	PR c++/15227
	* parser.c (cp_parser_direct_declarator): Robustify.
	
	PR c++/15877
	* pt.c (tsubst_copy): Use decl_constant_value on enumeration
	constants in non-dependent contexts.
	
	PR c++/14211
	PR c++/15076
	* typeck.c (build_static_cast): Wrap casts in NON_LVALUE_EXPR when
	necessary.
	
	PR c++/14211
	* g++.dg/conversion/const1.C: New test.
	
	PR c++/15076
	* g++.dg/conversion/reinterpret1.C: New test.
	
	PR c++/15877
	* g++.dg/template/enum2.C: New test.
	
	PR c++/15227
	* g++.dg/template/error13.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4089&r2=1.4090
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/parser.c.diff?cvsroot=gcc&r1=1.202&r2=1.203
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&r1=1.860&r2=1.861
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&r1=1.548&r2=1.549
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/conversion/const1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/conversion/reinterpret1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/enum2.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/error13.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 6 CVS Commits 2004-06-10 14:26:55 UTC
Subject: Bug 14211

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	mmitchel@gcc.gnu.org	2004-06-10 14:26:50

Modified files:
	gcc/cp         : ChangeLog parser.c pt.c typeck.c 
Added files:
	gcc/testsuite/g++.dg/conversion: const1.C reinterpret1.C 
	gcc/testsuite/g++.dg/template: enum2.C error13.C 

Log message:
	PR c++/15227
	* parser.c (cp_parser_direct_declarator): Robustify.
	
	PR c++/15877
	* pt.c (tsubst_copy): Use decl_constant_value on enumeration
	constants in non-dependent contexts.
	
	PR c++/14211
	PR c++/15076
	* typeck.c (build_static_cast): Wrap casts in NON_LVALUE_EXPR when
	necessary.
	
	PR c++/14211
	* g++.dg/conversion/const1.C: New test.
	
	PR c++/15076
	* g++.dg/conversion/reinterpret1.C: New test.
	
	PR c++/15877
	* g++.dg/template/enum2.C: New test.
	
	PR c++/15227
	* g++.dg/template/error13.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3892.2.119&r2=1.3892.2.120
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/parser.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.157.2.32&r2=1.157.2.33
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.816.2.29&r2=1.816.2.30
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.519.2.19&r2=1.519.2.20
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/conversion/const1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/conversion/reinterpret1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/enum2.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/error13.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1

Comment 7 Mark Mitchell 2004-06-10 14:30:18 UTC
Fixed in GCC 3.4.1.
Comment 8 Mark Mitchell 2004-06-10 14:30:43 UTC
*** Bug 15076 has been marked as a duplicate of this bug. ***