public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal
@ 2014-01-31 14:52 ed at catmur dot co.uk
  2014-06-25  4:21 ` [Bug c++/60009] " rs2740 at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ed at catmur dot co.uk @ 2014-01-31 14:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60009

            Bug ID: 60009
           Summary: g++ allows copy-initialization of an array of class
                    type from a non-braced string literal
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ed at catmur dot co.uk

The following code should be rejected, but is accepted by g++ 4.8.1, in both
C++03 and C++11 mode:

struct s { s(const char *); } a[] = "";

It is rejected by clang++ and MSVC.  Credit:
http://stackoverflow.com/questions/21481462/what-does-this-code-mean-and-why-does-it-work

List-initialization is correctly allowed in the braced-init-list case
(a[]{""}), and correctly disallowed in the parenthesized case (a[]("")).

Giving the array extent (a[2] = "") does not appear to make any difference.

It looks like the extent of the array is being taken from the length of the
string literal (including terminator), with some amusing results:

#include <string>
#include <iostream>
int main() {
  std::string a[] = "hello";
  for (int i = 0; i < sizeof(a)/sizeof(a[0]); ++i) std::cout << a[i] << '\n';
}

outputs:

hello
hello
hello
hello
hello
hello

(6, count 'em).

If an array extent is provided (e.g. std::string a[10] = "hello"), then the
string literal will be used to initialize each element.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/60009] g++ allows copy-initialization of an array of class type from a non-braced string literal
  2014-01-31 14:52 [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal ed at catmur dot co.uk
@ 2014-06-25  4:21 ` rs2740 at gmail dot com
  2014-12-14 14:16 ` ville.voutilainen at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rs2740 at gmail dot com @ 2014-06-25  4:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60009

TC <rs2740 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rs2740 at gmail dot com

--- Comment #1 from TC <rs2740 at gmail dot com> ---
http://stackoverflow.com/questions/24378882/weird-gcc-array-initialization-behavior
appears to be related.

Minimal example:

struct A { A() { } };
int main() { A a[10] = A(); }

Compiles with GCC 4.6, 4.7, 4.8 and 4.9, in both C++98 and C++11 modes, even
though it shouldn't. However,

struct A { };
int main() { A a[10] = A(); }

does not compile in any of the GCC versions tested.

struct A { A() = default; };
int main() { A a[10] = A(); }

Compiles with GCC 4.9 in C++11 mode; does not compile with 4.6-4.8 in C++ 11
mode, however.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/60009] g++ allows copy-initialization of an array of class type from a non-braced string literal
  2014-01-31 14:52 [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal ed at catmur dot co.uk
  2014-06-25  4:21 ` [Bug c++/60009] " rs2740 at gmail dot com
@ 2014-12-14 14:16 ` ville.voutilainen at gmail dot com
  2021-07-27 21:46 ` pinskia at gcc dot gnu.org
  2021-10-06 18:42 ` ppalka at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: ville.voutilainen at gmail dot com @ 2014-12-14 14:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60009

Ville Voutilainen <ville.voutilainen at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-12-14
                 CC|                            |ville.voutilainen at gmail dot com
     Ever confirmed|0                           |1
      Known to fail|                            |4.8.2, 4.9.1, 5.0

--- Comment #2 from Ville Voutilainen <ville.voutilainen at gmail dot com> ---
Clang rejects the code.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/60009] g++ allows copy-initialization of an array of class type from a non-braced string literal
  2014-01-31 14:52 [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal ed at catmur dot co.uk
  2014-06-25  4:21 ` [Bug c++/60009] " rs2740 at gmail dot com
  2014-12-14 14:16 ` ville.voutilainen at gmail dot com
@ 2021-07-27 21:46 ` pinskia at gcc dot gnu.org
  2021-10-06 18:42 ` ppalka at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-27 21:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60009

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=84632
         Resolution|---                         |FIXED
   Target Milestone|---                         |7.0
             Status|NEW                         |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed in GCC 7, I cannot figure out which revision fixed it though.

Note r8-7514 fixes the error message though where in GCC 7 would print out some
error messages dealing with no matching of constructors.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/60009] g++ allows copy-initialization of an array of class type from a non-braced string literal
  2014-01-31 14:52 [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal ed at catmur dot co.uk
                   ` (2 preceding siblings ...)
  2021-07-27 21:46 ` pinskia at gcc dot gnu.org
@ 2021-10-06 18:42 ` ppalka at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-10-06 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60009

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> Fixed in GCC 7, I cannot figure out which revision fixed it though.
> 
> Note r8-7514 fixes the error message though where in GCC 7 would print out
> some error messages dealing with no matching of constructors.

Fixed by r7-7236 FWIW

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-06 18:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-31 14:52 [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal ed at catmur dot co.uk
2014-06-25  4:21 ` [Bug c++/60009] " rs2740 at gmail dot com
2014-12-14 14:16 ` ville.voutilainen at gmail dot com
2021-07-27 21:46 ` pinskia at gcc dot gnu.org
2021-10-06 18:42 ` ppalka at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).