[Bug c++/33909] New: Error reported on a legal source: "invalid use of non-lvalue array" when array name is used in initializer
igusarov at mail dot ru
gcc-bugzilla@gcc.gnu.org
Fri Oct 26 14:34:00 GMT 2007
Compiling the following code with g++ result in error. Command line to
reproduce the error is "arm-linux-gnueabi-g++ -c test.cpp", where test.cpp is
the file comprising the following lines:
struct T
{
const void* ptr;
};
const struct T my_arr[] =
{
{ 0 },
{ my_arr+1 } // Error at this line
};
Error is: "invalid use of non-lvalue array"
This error is reported only when compiling as C++ code. Compilation with gcc or
with "-xc" is successful.
I think that the code above perfectly meet C++ standard because:
1. According to 3.3.1p1, the point of declaration of my_arr is immediately
before "=". Thus, the name "my_arr" is properly introduced and can be used in
the initializer.
2. Expression "my_arr+1" should be treated as: Array-to-pointer conversion
(4.2p1), additive operator + (5.7p1). Resulting pointer does not exceed array
boundaries, so the expression is well-formed and it has legal result.
3. Whether "my_arr" is an lvalue or an rvalue expression is not important,
because array-to-pointer conversion is defined for lvalue and rvalue arrays
(4.2p1). Which makes error message irrelevant, because such use is valid.
4. However, I believe that expression "my_arr" is an lvalue expression because
it refers to an object (3.10p2). The fact that the object is const (i.e.
non-modifiable) doesn't make it non-lvalue.
Output of "arm-linux-gnueabi-g++ -v" is:
Using built-in specs.
Target: arm-none-linux-gnueabi
Configured with:
/home/toolbuilder/tools_build/build.arm-linux-gnueabi/gcc-4.1.1/configure
--target=arm-none-linux-gnueabi
--prefix=/usr/toolchain/arm-linux-gnueabi/gcc-4.1.1
--with-local-prefix=/usr/toolchain/arm-linux-gnueabi/gcc-4.1.1/local
--with-sysroot=/usr/toolchain/arm-linux-gnueabi/sysroot-arm11-linux-2.6.17.8-glibc-2.4
--enable-languages=c,c++ --enable-c99
--program-transform-name=s/.*/arm-linux-gnueabi-&-4.1.1/ --enable-shared
--enable-__cxa_atexit --disable-libunwind-exceptions --enable-threads=posix
--enable-symvers=gnu --enable-long-long --disable-cpp --with-gnu-as
--with-gnu-ld
--with-as=/usr/toolchain/arm-linux-gnueabi/bin/arm-linux-gnueabi-as
--with-ld=/usr/toolchain/arm-linux-gnueabi/bin/arm-linux-gnueabi-ld
--with-nm=/usr/toolchain/arm-linux-gnueabi/bin/arm-linux-gnueabi-nm
--with-objdump=/usr/toolchain/arm-linux-gnueabi/bin/arm-linux-gnueabi-objdump
--disable-nls --disable-multilib
Thread model: posix
gcc version 4.1.1
--
Summary: Error reported on a legal source: "invalid use of non-
lvalue array" when array name is used in initializer
Product: gcc
Version: 4.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: igusarov at mail dot ru
GCC build triplet: i386-unknown-freebsd5.4
GCC host triplet: i386-unknown-freebsd5.4
GCC target triplet: arm-none-linux-gnueabi
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33909
More information about the Gcc-bugs
mailing list