public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "ed at catmur dot co.uk" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/60009] New: g++ allows copy-initialization of an array of class type from a non-braced string literal Date: Fri, 31 Jan 2014 14:52:00 -0000 [thread overview] Message-ID: <bug-60009-4@http.gcc.gnu.org/bugzilla/> (raw) 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.
next reply other threads:[~2014-01-31 14:52 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2014-01-31 14:52 ed at catmur dot co.uk [this message] 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-60009-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).