Bug 64665 - Overload resolution not working with std::initializer_list<std::string> and bool
Summary: Overload resolution not working with std::initializer_list<std::string> and bool
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-19 11:16 UTC by Duncan Forster
Modified: 2024-11-23 02:56 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-01-19 00:00:00


Attachments
gcc -v (845 bytes, text/plain)
2015-01-19 11:16 UTC, Duncan Forster
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Duncan Forster 2015-01-19 11:16:42 UTC
Created attachment 34482 [details]
gcc -v

Test case.

#include <string>
#include <iostream>

void Test1(const bool arg) 
{
    std::cout << "Right[" << arg << "] (standard conversion sequence)" << std::endl;
}
void Test1(const std::string arg)
{
    std::cout << "Wrong[" << arg.size() << "] (user-defined conversion sequence)" << std::endl;
}

void Test2(const int arg) 
{
    std::cout << "Wrong[" << arg << "] (no conversion, doesn't initializes std::initializer_list)" << std::endl;
}
void Test2(const std::initializer_list<int> arg)
{
    std::cout << "Right[" << arg.size() << "] (no conversion, initializes std::initializer_list)" << std::endl;
}

struct S 
{ 
    S(int _a) : a(_a){}
    int getA() const { return a; }
private:
    int a;
};
void Test3(const int arg) 
{
    std::cout << "Right[" << arg << "] (standard conversion sequence)" << std::endl;
}
void Test3(const S arg)
{
    std::cout << "Wrong[" << arg.getA() << "] (user-defined conversion sequence)" << std::endl;
}

void Test4(const bool arg) 
{
    std::cout << "Right[" << arg << "] (standard conversion sequence)" << std::endl;
}
void Test4(const std::initializer_list<std::string> arg)
{
    std::cout << "Wrong[" << arg.size() << "] (user-defined conversion sequence)" << std::endl;
}

int main (int /*argc*/, char * const /*argv*/[]) 
{
    Test1({"false"});
    Test2({123});
    Test3({456});
    Test4({"false"});
    return 0;
}

GCC should print:
  Right[1] (standard conversion sequence)
  Right[1] (no conversion, initializes std::initializer_list)
  Right[456] (standard conversion sequence)
  Right[1] (standard conversion sequence)

GCC prints this instead:
  Right[1] (standard conversion sequence)
  Right[1] (no conversion, initializes std::initializer_list)
  Right[456] (standard conversion sequence)
  Wrong[1] (user-defined conversion sequence)

Here are the two conversion sequences for Test4:
 
  {const char[6]} -> {const char*} -> {bool} (standard conversion sequence)
  {const char[6]} -> {const char*} -> {std::string::ctor} (user-defined conversion sequence)

The standard conversion sequence should be preferred over a user-defined conversion sequence.
Comment 1 Jonathan Wakely 2015-01-19 12:55:37 UTC
Possibly a duplicate of another bug but I can't find one right now.
Comment 2 Richard Smith 2015-01-21 02:40:25 UTC
Per DR1467, a list-initialization sequence that initializes std::initializer_list is always better than one that does not; I think this is invalid.
Comment 3 Duncan Forster 2015-01-21 04:35:15 UTC
(In reply to Richard Smith from comment #2)
I've been back and forth about what is correct behavior , in the end I think you're correct. GCC seems to be doing the right thing according to the latest C++ defect reports.
Comment 4 Jakub Jelinek 2015-01-21 08:43:09 UTC
Shouldn't we add the testcase to gcc testsuite nevertheless, or do we have this covered in the testsuite already?
Comment 5 Paolo Carlini 2015-03-05 09:07:30 UTC
Let's add a reduced testcase and close the bug.
Comment 6 paolo@gcc.gnu.org 2015-03-05 09:16:30 UTC
Author: paolo
Date: Thu Mar  5 09:15:58 2015
New Revision: 221207

URL: https://gcc.gnu.org/viewcvs?rev=221207&root=gcc&view=rev
Log:
2015-03-05  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/64665
	* g++.dg/cpp0x/initlist92.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/initlist92.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 7 Paolo Carlini 2015-03-05 09:22:52 UTC
Done.