public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string)
@ 2020-06-09 3:05 bzsurr at protonmail dot com
2020-06-09 7:17 ` [Bug c++/95596] " bzsurr at protonmail dot com
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: bzsurr at protonmail dot com @ 2020-06-09 3:05 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
Bug ID: 95596
Summary: string literal wrong overload resolution (char* vs
std::string)
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: bzsurr at protonmail dot com
Target Milestone: ---
Looking at the following code gcc calls the _char*_ overload, according to the
standard the string literal is of type const array of char, so the non const
char pointer should not be part of the overload set.
#include <cstdio>
#include <string>
void foo(char*) { puts("char*"); }
void foo(std::string) { puts("std::string"); }
int main()
{
foo("Hello World");
}
Compiler Explorer Link:
https://godbolt.org/z/82D4LT
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
@ 2020-06-09 7:17 ` bzsurr at protonmail dot com
2020-06-09 13:43 ` redi at gcc dot gnu.org
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: bzsurr at protonmail dot com @ 2020-06-09 7:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
--- Comment #1 from bzsurr at protonmail dot com ---
(In reply to bzsurr from comment #0)
> Looking at the following code gcc calls the _char*_ overload, according to
> the standard the string literal is of type const array of char, so the non
> const char pointer should not be part of the overload set.
>
> #include <cstdio>
> #include <string>
> void foo(char*) { puts("char*"); }
> void foo(std::string) { puts("std::string"); }
> int main()
> {
> foo("Hello World");
> }
>
>
> Compiler Explorer Link:
> https://godbolt.org/z/82D4LT
Compiler output:
<source>: In function 'int main()':
<source>:9:9: warning: ISO C++ forbids converting a string constant to 'char*'
[-Wwrite-strings]
9 | foo("Hello World");
| ^~~~~~~~~~~~~
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
2020-06-09 7:17 ` [Bug c++/95596] " bzsurr at protonmail dot com
@ 2020-06-09 13:43 ` redi at gcc dot gnu.org
2020-06-09 13:45 ` redi at gcc dot gnu.org
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-09 13:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Ever confirmed|0 |1
Last reconfirmed| |2020-06-09
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Confirmed. Even with -std=c++14 -pedantic-errors we still choose the wrong
overload (and then give an error that the conversion is not allowed).
There seems to be no way to disable the conversion from string literals to
char* that has been deprecated for years.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
2020-06-09 7:17 ` [Bug c++/95596] " bzsurr at protonmail dot com
2020-06-09 13:43 ` redi at gcc dot gnu.org
@ 2020-06-09 13:45 ` redi at gcc dot gnu.org
2020-06-10 19:30 ` mpolacek at gcc dot gnu.org
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-09 13:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
gcc/cp/typeck.c does:
if (cxx_dialect >= cxx11)
pedwarn (loc, OPT_Wwrite_strings,
"ISO C++ forbids converting a string constant to %qT",
totype);
else
warning_at (loc, OPT_Wwrite_strings,
"deprecated conversion from string constant to %qT",
totype);
As the example above shows, allowing the conversion and then conditionally
warning about it is not conforming.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
` (2 preceding siblings ...)
2020-06-09 13:45 ` redi at gcc dot gnu.org
@ 2020-06-10 19:30 ` mpolacek at gcc dot gnu.org
2020-07-22 14:05 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-06-10 19:30 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |mpolacek at gcc dot gnu.org
--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
So this is essentially:
struct S {
S(const char *);
};
void foo(char *) = delete;
void foo(S);
void
g ()
{
foo ("");
}
which compiles with clang++ but not with gcc.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
` (3 preceding siblings ...)
2020-06-10 19:30 ` mpolacek at gcc dot gnu.org
@ 2020-07-22 14:05 ` redi at gcc dot gnu.org
2020-07-22 14:06 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-07-22 14:05 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Another one that G++ rejects:
void f(char*);
int &f(...);
int &r = f("foo");
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
` (4 preceding siblings ...)
2020-07-22 14:05 ` redi at gcc dot gnu.org
@ 2020-07-22 14:06 ` redi at gcc dot gnu.org
2022-02-14 20:11 ` pinskia at gcc dot gnu.org
2023-05-27 17:58 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-07-22 14:06 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |zhonghao at pku dot org.cn
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 86498 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
` (5 preceding siblings ...)
2020-07-22 14:06 ` redi at gcc dot gnu.org
@ 2022-02-14 20:11 ` pinskia at gcc dot gnu.org
2023-05-27 17:58 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-14 20:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |johnsen.david at siemens dot com
--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 104534 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/95596] string literal wrong overload resolution (char* vs std::string)
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
` (6 preceding siblings ...)
2022-02-14 20:11 ` pinskia at gcc dot gnu.org
@ 2023-05-27 17:58 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-27 17:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95596
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |arthur.j.odwyer at gmail dot com
--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 110005 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-05-27 17:58 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-09 3:05 [Bug c++/95596] New: string literal wrong overload resolution (char* vs std::string) bzsurr at protonmail dot com
2020-06-09 7:17 ` [Bug c++/95596] " bzsurr at protonmail dot com
2020-06-09 13:43 ` redi at gcc dot gnu.org
2020-06-09 13:45 ` redi at gcc dot gnu.org
2020-06-10 19:30 ` mpolacek at gcc dot gnu.org
2020-07-22 14:05 ` redi at gcc dot gnu.org
2020-07-22 14:06 ` redi at gcc dot gnu.org
2022-02-14 20:11 ` pinskia at gcc dot gnu.org
2023-05-27 17:58 ` pinskia 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).