public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector
@ 2024-02-12  4:51 jlame646 at gmail dot com
  2024-02-12  4:58 ` [Bug c++/113884] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jlame646 at gmail dot com @ 2024-02-12  4:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113884
           Summary: GCC rejects valid program saying ambiguous call when
                    using std::vector
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlame646 at gmail dot com
  Target Milestone: ---

The following valid program is rejected by gcc
```
#include <type_traits>
#include <vector>

struct A {
    A();
};


void func(std::vector<double> values);
void func(std::vector<A> as);

int main() {
    func({ 4.2 }); //gcc rejects this
}
```

One way to see that gcc is incorrect here is by just removing the first
overload and then gcc also starts rejecting this, implying the the second
overload is not even viable so how can it make the call ambiguous.

```
#include <type_traits>
#include <vector>

struct A {
    A();
};


void func(std::vector<A> as);

int main() {
    func({ 4.2 });//now gcc also starts correctly rejecting this implying that
this is not even viable so how can it possibly make the call ambiguous 
}
```

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
@ 2024-02-12  4:58 ` pinskia at gcc dot gnu.org
  2024-02-12  5:01 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-12  4:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced to:
```
struct B
{
  B(double);
};

struct C
{
  C(int);
};


void func(B);
void func(C);

int main() {
    func({ 4.2 });
}
```

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
  2024-02-12  4:58 ` [Bug c++/113884] " pinskia at gcc dot gnu.org
@ 2024-02-12  5:01 ` pinskia at gcc dot gnu.org
  2024-02-12  5:03 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-12  5:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Reduced to:
> ```
> struct B
> {
>   B(double);
> };
> 
> struct C
> {
>   C(int);
> };
> 
> 
> void func(B);
> void func(C);
> 
> int main() {
>     func({ 4.2 });
> }
> ```

Actually C constructor should be:
  explicit  C(int);

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
  2024-02-12  4:58 ` [Bug c++/113884] " pinskia at gcc dot gnu.org
  2024-02-12  5:01 ` pinskia at gcc dot gnu.org
@ 2024-02-12  5:03 ` pinskia at gcc dot gnu.org
  2024-02-12  5:18 ` jlame646 at gmail dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-12  5:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Since explicit is needed, then this is a dup of bug 60027 which means GCC is
correct at rejecting this as being ambigous.  Clang does not implement
https://wg21.link/cwg1228 resolution.

*** This bug has been marked as a duplicate of bug 60027 ***

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
                   ` (2 preceding siblings ...)
  2024-02-12  5:03 ` pinskia at gcc dot gnu.org
@ 2024-02-12  5:18 ` jlame646 at gmail dot com
  2024-02-12  5:22 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jlame646 at gmail dot com @ 2024-02-12  5:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jason Liam <jlame646 at gmail dot com> ---
But which constructor is explicit here? I don't see any explicit ctor here.

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
                   ` (3 preceding siblings ...)
  2024-02-12  5:18 ` jlame646 at gmail dot com
@ 2024-02-12  5:22 ` pinskia at gcc dot gnu.org
  2024-02-12  5:25 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-12  5:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jason Liam from comment #4)
> But which constructor is explicit here? I don't see any explicit ctor here.

std::vector 's constructor which takes std::size_t is marked as explicit.

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
                   ` (4 preceding siblings ...)
  2024-02-12  5:22 ` pinskia at gcc dot gnu.org
@ 2024-02-12  5:25 ` pinskia at gcc dot gnu.org
  2024-02-12  5:47 ` jlame646 at gmail dot com
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-12  5:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I know cppreference is not the standard but it is a decent reference to start
with.

https://en.cppreference.com/w/cpp/container/vector/vector


Without the explicit, clang will also reject it as being ambiguous. As I
mentioned this is not a bug in gcc but rather a bug in clang.

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
                   ` (5 preceding siblings ...)
  2024-02-12  5:25 ` pinskia at gcc dot gnu.org
@ 2024-02-12  5:47 ` jlame646 at gmail dot com
  2024-02-12  5:59 ` pinskia at gcc dot gnu.org
  2024-02-12  7:58 ` jlame646 at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: jlame646 at gmail dot com @ 2024-02-12  5:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jason Liam <jlame646 at gmail dot com> ---
(In reply to Andrew Pinski from comment #5)
> std::vector 's constructor which takes std::size_t is marked as explicit.

But you're missing that the initializer list ctor is preferred/choosen over the
size_t arg ctor. 

See
https://stackoverflow.com/questions/27144054/why-is-the-stdinitializer-list-constructor-preferred-when-using-a-braced-initi

I've created the following demo to show this:

```

class Foo
{
    //  val_;
public:
    Foo(std::initializer_list<double> il)
    {
        std::cout << "initializer_list ctor" << std::endl;
    }
     explicit  Foo(std::size_t val)
    {
        std::cout << "ctor" << std::endl;
    };
};

int main()
{
    Foo f = {1.1}; //all compiler use initializer list
}
```

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
                   ` (6 preceding siblings ...)
  2024-02-12  5:47 ` jlame646 at gmail dot com
@ 2024-02-12  5:59 ` pinskia at gcc dot gnu.org
  2024-02-12  7:58 ` jlame646 at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-12  5:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jason Liam from comment #7)
> (In reply to Andrew Pinski from comment #5)
> > std::vector 's constructor which takes std::size_t is marked as explicit.
> 
> But you're missing that the initializer list ctor is preferred/choosen over
> the size_t arg ctor. 

That is different from your original example.
This is closer to your original example:
```
#include <initializer_list>

struct B
{
  B(std::initializer_list<double>);
};

struct C
{
  explicit C(int);
};


void func(B);
void func(C);

int main() {
    func({ 4.2 });
}
```

here we have two calls to func, one which takes B and the other which takes C.
In the case case of copy-list-initialization, it is ambigous which is to be
constructed as copy-list-initialization for overload resolution; explicit
constructors are looked at but only an error if it was chosen. Note narrowing
is not taken into account for overload resolution too; maybe that is what you
are missing.

Again read https://wg21.link/cwg1228 which talks about this not being a defect
in the C++ standard; this is how the standard is written.

Anyways clang's bug report is https://github.com/llvm/llvm-project/issues/28016
.

>implying the the second overload is not even viable so how can it make the call ambiguous.

I should note that is not have overload resolution works either.

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

* [Bug c++/113884] GCC rejects valid program saying ambiguous call when using std::vector
  2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
                   ` (7 preceding siblings ...)
  2024-02-12  5:59 ` pinskia at gcc dot gnu.org
@ 2024-02-12  7:58 ` jlame646 at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: jlame646 at gmail dot com @ 2024-02-12  7:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Jason Liam <jlame646 at gmail dot com> ---
(In reply to Andrew Pinski from comment #8)
Does that imply that following program is also invalid? GCC rejects the below
program but msvc accepts.

```
struct A
{
  explicit A(int = 10);
  A()= default;
};

A a = {}; //gcc rejects this but msvc accepts
```

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

end of thread, other threads:[~2024-02-12  7:58 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-12  4:51 [Bug c++/113884] New: GCC rejects valid program saying ambiguous call when using std::vector jlame646 at gmail dot com
2024-02-12  4:58 ` [Bug c++/113884] " pinskia at gcc dot gnu.org
2024-02-12  5:01 ` pinskia at gcc dot gnu.org
2024-02-12  5:03 ` pinskia at gcc dot gnu.org
2024-02-12  5:18 ` jlame646 at gmail dot com
2024-02-12  5:22 ` pinskia at gcc dot gnu.org
2024-02-12  5:25 ` pinskia at gcc dot gnu.org
2024-02-12  5:47 ` jlame646 at gmail dot com
2024-02-12  5:59 ` pinskia at gcc dot gnu.org
2024-02-12  7:58 ` jlame646 at gmail dot com

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