public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal
@ 2023-12-22  4:18 jlame646 at gmail dot com
  2023-12-22  5:07 ` [Bug c++/113110] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: jlame646 at gmail dot com @ 2023-12-22  4:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113110
           Summary: GCC rejects call to more specialized const char array
                    version with string literal
           Product: gcc
           Version: 13.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlame646 at gmail dot com
  Target Milestone: ---

GCC seems to rejects valid code. The following code is rejected by gcc and
clang while accepted by msvc. As the first version is more specialized for
const char arrays, it seems the first version should be used.
https://godbolt.org/z/anEnY44Te

```
#include <cstring>

using namespace std;

// fist version
template <size_t N, size_t M>
int compare(const char (&a)[N], const char (&b)[M]) {
    return strcmp(a, b);
}

// second version
template <typename T>
int compare(const T &a, const T &b) {
    if (a < b) return -1;
    if (b < a) return 1;
    return 0;
}

int main() {
    compare("dog", "cat"); //gcc and clang rejects while msvc accepts
}
```

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

* [Bug c++/113110] GCC rejects call to more specialized const char array version with string literal
  2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
@ 2023-12-22  5:07 ` pinskia at gcc dot gnu.org
  2023-12-22  5:17 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-22  5:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
EDG 6.5 also rejects the code:
```
<source>(21): error: more than one instance of overloaded function "compare"
matches the argument list:
            function template "int compare(const char (&)[N], const char
(&)[M])" (declared at line 8)
            function template "int compare(const T &, const T &)" (declared at
line 14)
            argument types are: (const char [4], const char [4])
      compare("dog", "cat"); //gcc and clang rejects while msvc accepts
      ^

```

I suspect this is either a bug or an extension for MSVC.

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

* [Bug c++/113110] GCC rejects call to more specialized const char array version with string literal
  2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
  2023-12-22  5:07 ` [Bug c++/113110] " pinskia at gcc dot gnu.org
@ 2023-12-22  5:17 ` pinskia at gcc dot gnu.org
  2023-12-22  5:18 ` jlame646 at gmail dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-22  5:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am almost want to say MSVC never implmented DR 214 which addresses this.


https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#214

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

* [Bug c++/113110] GCC rejects call to more specialized const char array version with string literal
  2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
  2023-12-22  5:07 ` [Bug c++/113110] " pinskia at gcc dot gnu.org
  2023-12-22  5:17 ` pinskia at gcc dot gnu.org
@ 2023-12-22  5:18 ` jlame646 at gmail dot com
  2023-12-22  5:21 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jlame646 at gmail dot com @ 2023-12-22  5:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jason Liam <jlame646 at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)

> I suspect this is either a bug or an extension for MSVC.

Are you sure? I mean if you add another template parameter `U` to the second
parameter and use it then gcc starts accepting the code and using the more
specialized version. Demo:https://godbolt.org/z/W7Ma6c5Ts

This indicates that gcc was wrong in rejecting the original code. For reference
here is the modified code which starts compiling with gcc: 

```
template <size_t N, size_t M>
int compare(const char (&a)[N], const char (&b)[M]) {
    return strcmp(a, b);
}
//--------------------vvvvvvvvvv-------->added this parameter
template <typename T, typename U>
//----------------------------v--------->changed this to U
int compare(const T &a, const U &b) {
    if (a < b) return -1;
    if (b < a) return 1;
    return 0;
}

int main() {
    compare("dog", "cat"); //now gcc also accepts it
}
```

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

* [Bug c++/113110] GCC rejects call to more specialized const char array version with string literal
  2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
                   ` (2 preceding siblings ...)
  2023-12-22  5:18 ` jlame646 at gmail dot com
@ 2023-12-22  5:21 ` pinskia at gcc dot gnu.org
  2023-12-22  5:33 ` pinskia at gcc dot gnu.org
  2023-12-22 10:19 ` harald at gigawatt dot nl
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-22  5:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jason Liam from comment #3)
> (In reply to Andrew Pinski from comment #1)
> 
> > I suspect this is either a bug or an extension for MSVC.
> 
> Are you sure? I mean if you add another template parameter `U` to the second
> parameter and use it then gcc starts accepting the code and using the more
> specialized version. Demo:https://godbolt.org/z/W7Ma6c5Ts


It is 3 compilers vs 1 here. Either there is some defect against the C++
standard or the odd one out has an issue ...

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

* [Bug c++/113110] GCC rejects call to more specialized const char array version with string literal
  2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
                   ` (3 preceding siblings ...)
  2023-12-22  5:21 ` pinskia at gcc dot gnu.org
@ 2023-12-22  5:33 ` pinskia at gcc dot gnu.org
  2023-12-22 10:19 ` harald at gigawatt dot nl
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-22  5:33 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=15674

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=15674 also.

I am still suspecting MSVC of not implementing the C++ Defect report 214 .

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

* [Bug c++/113110] GCC rejects call to more specialized const char array version with string literal
  2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
                   ` (4 preceding siblings ...)
  2023-12-22  5:33 ` pinskia at gcc dot gnu.org
@ 2023-12-22 10:19 ` harald at gigawatt dot nl
  5 siblings, 0 replies; 7+ messages in thread
From: harald at gigawatt dot nl @ 2023-12-22 10:19 UTC (permalink / raw)
  To: gcc-bugs

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

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #6 from Harald van Dijk <harald at gigawatt dot nl> ---
(In reply to Jason Liam from comment #3)
> Are you sure? I mean if you add another template parameter `U` to the second
> parameter and use it then gcc starts accepting the code and using the more
> specialized version. Demo:https://godbolt.org/z/W7Ma6c5Ts

In order to determine whether int compare(const char (&)[N], const char (&)[M])
is more specialised than int compare(const T &, const T &), template argument
deduction is attempted to solve one in terms of the other. Solving gives T =
char[N] for the first parameter, but T = char[M] for the second parameter. This
is a conflict that is ignored by MSVC.

When adding the second template parameter `U`, solving gives T = char[N], and U
= char[M]. This is never a conflict, this unambiguously makes the array version
more specialised.

(In reply to Andrew Pinski from comment #5)
> I am still suspecting MSVC of not implementing the C++ Defect report 214 .

At first glance, both GCC/clang and MSVC behaviour look like legitimate but
different interpretations of DR 214. Whether MSVC is right to ignore that
conflict is an open question covered by
https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2160.

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

end of thread, other threads:[~2023-12-22 10:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-22  4:18 [Bug c++/113110] New: GCC rejects call to more specialized const char array version with string literal jlame646 at gmail dot com
2023-12-22  5:07 ` [Bug c++/113110] " pinskia at gcc dot gnu.org
2023-12-22  5:17 ` pinskia at gcc dot gnu.org
2023-12-22  5:18 ` jlame646 at gmail dot com
2023-12-22  5:21 ` pinskia at gcc dot gnu.org
2023-12-22  5:33 ` pinskia at gcc dot gnu.org
2023-12-22 10:19 ` harald at gigawatt dot nl

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).