Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang ignores 'explicit' constructors in copy-list-initialization of function argument #28016

Open
ecatmur opened this issue May 4, 2016 · 3 comments
Labels
bugzilla Issues migrated from bugzilla c++11 clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@ecatmur
Copy link

ecatmur commented May 4, 2016

Bugzilla Link 27642
Version trunk
OS All
CC @DougGregor,@romkatv

Extended Description

Per N2532=08-0042 (http://www.stroustrup.com/list-issues-2.pdf) chapter 4 the following program should be rejected as ambiguous:

struct String1 { explicit String1(const char*); };
struct String2 { String2(const char*); }; 

void f(String1);
void f(String2);

int main() {
 f( {"asdf"} );  // error, ambiguous
} 

This was confirmed by CWG 1228 (http://wg21.cmeerw.net/cwg/issue1228):

    struct MyStore {
      explicit MyStore(int initialCapacity);
    };

    struct MyInt {
      MyInt(int i);
    };

    struct Printer {
      void operator()(MyStore const& s);
      void operator()(MyInt const& i);
    };

    void f() {
      Printer p;
      p({23});  // error, ambiguous
    }

gcc (6.1 and preceding versions) correctly rejects both these programs.

I don't like this behavior, but it is what over.match.list prescribes.

Possible follow up to bug 12120.

Discussion: http://stackoverflow.com/questions/34622076/calling-an-explicit-constructor-with-a-braced-init-list-ambiguous-or-not?rq=1

@romkatv
Copy link

romkatv commented Jan 25, 2017

Simplified example:

struct X {
  explicit X() {}
};

struct Y {};

void F(X) {}
void F(Y) {}

int main() {
  // GCC: error: call of overloaded 'F(<brace-enclosed initializer list>)'
  //             is ambiguous.
  //
  // Clang: OK, calls F(Y).
  F({});
}

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
@shafik
Copy link
Collaborator

shafik commented Feb 13, 2024

It looks like gcc is the only one that implements this DR: https://godbolt.org/z/Ea19M1vc1

CC @zygoloid @erichkeane

This does feel a bit unintuitive to me.

@EugeneZelenko EugeneZelenko added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Feb 13, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 13, 2024

@llvm/issue-subscribers-clang-frontend

Author: Ed Catmur (ecatmur)

| | | | --- | --- | | Bugzilla Link | [27642](https://llvm.org/bz27642) | | Version | trunk | | OS | All | | CC | @DougGregor,@romkatv |

Extended Description

Per N2532=08-0042 (http://www.stroustrup.com/list-issues-2.pdf) chapter 4 the following program should be rejected as ambiguous:

struct String1 { explicit String1(const char*); };
struct String2 { String2(const char*); };

void f(String1);
void f(String2);

int main() {
f( {"asdf"} ); // error, ambiguous
}

This was confirmed by CWG 1228 (http://wg21.cmeerw.net/cwg/issue1228):

struct MyStore {
  explicit MyStore(int initialCapacity);
};

struct MyInt {
  MyInt(int i);
};

struct Printer {
  void operator()(MyStore const&amp; s);
  void operator()(MyInt const&amp; i);
};

void f() {
  Printer p;
  p({23});  // error, ambiguous
}

gcc (6.1 and preceding versions) correctly rejects both these programs.

I don't like this behavior, but it is what over.match.list prescribes.

Possible follow up to bug 12120.

Discussion: http://stackoverflow.com/questions/34622076/calling-an-explicit-constructor-with-a-braced-init-list-ambiguous-or-not?rq=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++11 clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

5 participants