Bug 20140 - template function complains about "char-array initialized from wide string"
Summary: template function complains about "char-array initialized from wide string"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P2 normal
Target Milestone: 4.7.0
Assignee: Paolo Carlini
URL:
Keywords: monitored, rejects-valid
Depends on:
Blocks:
 
Reported: 2005-02-22 12:21 UTC by Lukas Mai
Modified: 2012-01-02 16:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-09-03 21:39:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lukas Mai 2005-02-22 12:21:44 UTC
Hello,

this is my code:

template<typename T> void foo() {
    unsigned char s[] = "";
    (void)s;
}

int main() {
    /* removing either of the calls below makes the error disappear */
    foo<int>();
    foo<char>();
}

Compiling this program with g++[0] results in
try.cc: In function `void foo() [with T = char]':
try.cc:9:   instantiated from here
try.cc:2: error: char-array initialized from wide string

I think this is wrong because "" isn't a wide string. The error disappears
when I remove "unsigned" from the declaration of s, or when only a single
foo<T> is instantiated. The actual types don't matter; my original code
used function pointers.

HTH, Lukas
[0] g++ versions:
Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.3/specs
Configured with: ../gcc-3.4.3/configure --with-cpu=i686 --with-arch=i686 --with-tune=i686 --enable-__cxa_atexit
Thread model: posix
gcc version 3.4.3

Reading specs from /usr/local/dist/bin/../lib/gcc-lib/sparc-sun-solaris2.8/3.2.2/specs
Configured with: /compilefs/usr.local.compile/dist/fenk/gcc-3.2.2/configure --prefix=/usr/local/dist/DIR/GNU/gcc-3.2.2 --with-gnu-as --with-as=/usr/local/dist/DIR/GNU/binutils/bin/as --with-gnu-ld --with-ld=/usr/local/dist/DIR/GNU/binutils/bin/ld --with-dwarf2 --disable-multilib --with-local-prefix=/usr/local/dist
Thread model: posix
gcc version 3.2.2
Comment 1 Volker Reichelt 2005-02-22 12:56:16 UTC
Confirmed.
This fails since gcc 2.95.3 or earlier.
Comment 2 Lukas Mai 2008-03-13 09:28:35 UTC
Heh, this "new" bug still exists in 4.3.0.
Comment 3 Paolo Carlini 2012-01-02 00:42:34 UTC
On it. I see what's going wrong, fixing it should be relatively easy.
Comment 4 paolo@gcc.gnu.org 2012-01-02 16:15:55 UTC
Author: paolo
Date: Mon Jan  2 16:15:49 2012
New Revision: 182805

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=182805
Log:
/cp
2012-01-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/20140
	* typeck2.c (digest_init_r): Use copy_init when initializing
	an array of chars.

/testsuite
2012-01-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/20140
	* g++.dg/template/init9.C: New.


Added:
    trunk/gcc/testsuite/g++.dg/template/init9.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck2.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Paolo Carlini 2012-01-02 16:16:52 UTC
Fixed for 4.7.0.