Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #83879

Re: Uniform initialization ambiguity

From Andrey Tarasevich <andreytarasevich@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Uniform initialization ambiguity
Date 2022-04-29 08:42 -0700
Organization A noiseless patient Spider
Message-ID <t4h109$hal$1@dont-email.me> (permalink)
References <t4g5no$raf$1@gioia.aioe.org>

Show all headers | View raw


On 4/29/2022 12:56 AM, Juha Nieminen wrote:
> Suppose you have this overloaded function:
> 
>      void foobar(const std::string&);
>      void foobar(const std::vector<int>&);
> 
> and you try to call it like:
> 
>      foobar({});
> 
> you'll rather obviously get a compiler error because the call is ambiguous.
> There's no way for the compiler to know which one you want to call.
> 
> However, if the functions were instead like this:
> 
>      void foobar(const std::string&);
>      void foobar(int);
> 
> now that 'foobar({});' will compile just fine (at least with gcc), and
> call the foobar(int) function. How so? Why is there no ambiguity here?

Well, the rules for "List-initialization sequence [over.ics.list]" are 
rather convoluted

    http://eel.is/c++draft/over.ics.list

but one can figure out that the initialization of to `std::string` is 
classified as "a user-defined conversion sequence"

   http://eel.is/c++draft/over.ics.list#7.2

while initialization of an `int` is "an identity conversion"

   http://eel.is/c++draft/over.ics.list#10.2

Obviously, the latter wins over the former.

-- 
Best regards,
Andrey Tarasevich

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Uniform initialization ambiguity Juha Nieminen <nospam@thanks.invalid> - 2022-04-29 07:56 +0000
  Re: Uniform initialization ambiguity Öö Tiib <ootiib@hot.ee> - 2022-04-29 06:29 -0700
    Re: Uniform initialization ambiguity "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-04-29 19:30 +0200
    Re: Uniform initialization ambiguity Juha Nieminen <nospam@thanks.invalid> - 2022-04-29 19:54 +0000
  Re: Uniform initialization ambiguity Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-04-29 08:42 -0700
    Re: Uniform initialization ambiguity Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-04-29 08:48 -0700

csiph-web